Checking my Google Analytics I'm getting a lot of traffic from semalt.com.
I know this is a traffic spam tactic, so I want to completely block their requests and stop it from turning up in my traffic stats.
I read here http://logorrhoea.net/2014/01/how-to-block-semalt-com-referrer-traffic-using-htaccess/ that on Apache it can be done like:
# block visitors referred from semalt.com
RewriteEngine on
RewriteCond %{HTTP_REFERER} semalt\.com [NC]
RewriteRule .* - [F]
but I need it for IIS, probably via the web.config, but how to do it?
Ok, found something here: http://tusksoft.com/blog/posts/6/clean-up-your-google-analytics-and-help-stop-referer-spam-with-this-simple-rewrite-rule:
This seems to work:
<rewrite>
<rules>
<rule name="abort referer spam requests" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_REFERER}" pattern="(semalt\.com)|(buttons\-for\-website\.com)" />
</conditions>
<action type="AbortRequest" />
</rule>
<!--The rest of your rules, if you have any-->
</rules>
</rewrite>
Try this, it should work:
<rewrite>
<rules>
<rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_REFERER}" matchType="Pattern" pattern="*.semalt.com*"
ignoreCase="true" negate="false" />
</conditions>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
I have tested and verified this but for a referrer URL other than *.semalt.com. The key in this code, different from your code, is a wildcard at the end of the referrer pattern because the referer URL ends with a "/". You can also replace the wildcard at the end with a "/" though I think a wildcard should be better.
What follows is a sample web.config with a equivalent URL Rewrite configuration to the one you have provided.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="block semalt referer" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_REFERER}" pattern="*.semalt.com" />
</conditions>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Related
i'm trying to force a url redirect (with the url rewrite module but for some reason it doesn't seem to be willing to match..)
my use case is as follows:
url: http://somesite.domain.com/?test=4
Now I'm trying to redirect this when it matches ?test=4 to http://somesite.domain.com/subsite/?test=5
I've tried this in multiple ways aleady by having a matching pattern of ^\?test=4 (on both the match url or the condition)
For some reason tho it isn't willing to trigger.
Is there something obviously wrong with a matching regex pattern of \?test=4 or should this work?
Full web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="^\?test=4" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?test=4" />
<action type="Redirect" url="/subsite/?test=5" appendQueryString="true" />
<conditions>
</conditions>
</rule>
<rule name="2" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAny">
</conditions>
<action type="Redirect" url="/subsite/?test=5" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I'm fairly now to the url rewrite module so I'm going a bit by trial and error..
Any tips would be nice!
As far as I know, the url match will not match the query string. So your url rewrite rule will not work.
If you want to check the querystring, I suggest you could use IIS url rewrite querystring condition.
More details, you could refer to below url rewrite rule:
<rule name="MatchQueryString" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="test=4" />
</conditions>
<action type="Redirect" url="/subsite/?test=5" appendQueryString="false" />
</rule>
Result:
Currently I have a sub-domain https://blog.example.com which I would like to redirect to https://example.com/blog. I'm using Craft CMS running on IIS 7 server.
I tried the code mentioned below but it didn't work:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect blog to new url" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern=".*" />
</conditions>
<action type="Redirect" url="https://example.com/blog/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Update: Adding directory structure
The problem seems to be with the condition you have specified. Your current pattern is .* which will result in a infinite loop. Change the pattern to check for the specific URL as below.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect blog to new url" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern=".*blog\.example\.com.*" />
</conditions>
<action type="Redirect" url="https://example.com/blog/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I tried this and it works for me. Go to http://blog.kaushal.co.in
If you need to troubleshoot URL Rewrite rules, then enable Failed Request tracing for your site. See this for more details: Using Failed Request Tracing to Trace Rewrite Rules
UPDATE
The rewrite rules need to be added to the 117072616095252 present at the site's root "/" and not in a web.config of one of the child folders.
On my web.config file, first I need to redirect users requesting URL on http to https. Then, afterwards, I need to do further stuff, I need to make sure www is there in the beginning of the URL. When redirecting users from non-www to www, I want to use the https already set on the previous rule. But I want to do it dinamically, I don't want to hardcode 'https://' on the beginning of my rewrite rule.
The goal is: in the future, if for some reason I need to change this rule from https back to http (I don't think I will, but I may need this), I don't have to go on checking all the redirect rules below to change from 'https' to 'http'. If I use a variable (not sure I should call things inside brackets like {HTTPS} as 'variables'), all I have to do is change the first rule and ignore the rest.
Here is an example so things can get clearer; the key issue I am trying to solve is the item where I have written {WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<rewrite>
<rules>
<!-- The first rule will do a 301 redirect to https -->
<rule name="Redirect to https" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<!-- Now, let's make sure we have www if request does not carry it -->
<rule name="Redirect to www" stopProcessing="false">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="{WHAT TO USE HERE TO MAKE SURE I USE THE SAME PROTOCOL (HTTP/HTTPS) ALREADY DEFINED ABOVE?}www.mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</location>
</configuration>
Thank you so much, any help appreciated.
Best way is to use rewrite map to identify protocol. In your case it will be like that:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="false">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.mydomain.com/{R:1}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
This rule seems to work fine, but when I go to https://www.example.com it doesn't remove the www.
However, if I go to http://www.example.com it works as expected.
Why is it not redirecting from https?
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$"/>
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Per my comment above, your rule actually worked for me in the scenario that you described. However, I do not think that it handles the case when a request is made to an insecure url that does not contain www in the hostname. For example, http://example.com/blah/ would not be redirected. The following rule below handles that case as well:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW and Ensure HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://example.com{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The rule checks for a hostname starting with www or an insecure request. If either condition is met, the user is redirected.
I have some old URLs in a forum application points to http://mydomain.tld/showthread.php?p=xxxx where xxxx is the an integer id.
My new forum application uses viewtopic.php?p=xxxx and I want to handle this using IIS web.config rewrite rules.
I have tried to add the last rule as the following:
<rewrite>
<rules>
<rule name="Redirect www to non www" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://domain.tld/{R:1}" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.tld" negate="true" />
</conditions>
</rule>
<!-- this for change from showthread.php to viewtopic.php -->
<rule name="Rewrite" stopProcessing="true">
<match url="(.*)showthread\.php" />
<action type="Rewrite" url="viewtopic\.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
However, it does not able to redirect to the viewtopic.php. Any suggestion?
I have to enable httpRedirect in the IIS. Then I used this redirect rule in the web.config at the root of my forum:
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*showthread.php" destination="/viewtopic.php?$P" />
</httpRedirect>