ASP.Net rewrite rule from one domain to a similar domain - asp.net

I am trying to write a rewrite rule that will take www.mydomain1.co.uk/* and rewrite to www.mydomain2.co.uk/*
I have the following rule that isn't working as I suspect it gets into a infinite loop.
Can anyone help please
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^mydomain1\.co\.uk/$" />
</conditions>
<action type="Redirect" url="http://www.mydomain10.co.uk/{R:1}" redirectType="Permanent" />
</rule>

try once following code
<rule name="Canonical Host Name">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain1\.co\.uk" />
</conditions>
<action type="Redirect" url="http://www.mydomain10.co.uk/{R:1}" redirectType="Permanent" />
</rule>

Related

ASP.Net - HTTP to HTTPS Rewrite Module Not Working When the Character '&' Exists in the URL String

My URL HTTP to HTTPS rewrite does not work when there is the character '&' in the URL.
A 410 page not found results.
An example that results in a 410 is...
http://curtainsmadesimple.co.uk/1062/Rapture-&-Wright/Rapture-&-Wright-Collection-Roomshots
OR
http://www.curtainsmadesimple.co.uk/1062/Rapture-&-Wright/Rapture-&-Wright-Collection-Roomshots
However if the '&' is replaced by 'and' in the above URLs the page loads correctly.
I have two rules to rewrite HTTP to HTTPS. One is where there is a non www and the other if there is a www
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.curtainsmadesimple\.co\.uk$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.curtainsmadesimple.co.uk/{R:1}" />
<rule name="Redirect to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
Can anyone tell me why I am getting this issue?
Thanks in advance.
It will work I believe. This is working for me.
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<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>
</rules>

Possible to exclude subfolder from https rewrite?

I have a webiste that runs https. Now some programmers need a subfoder that has no https.
This is what i have in my web.config right now:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
We need the folder "wp-content\rest" to be excluded from this https redirect.
In hope someone can help me with this one.
You need to add an additional condition into your rule:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{REQUEST_URI}" pattern="^/wp-content/rest" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Then this rule will disable redirects to all urls like that http://www.example.com/wp-content/rest*

Web.config URL rewrite - force www prefix and https

I'm trying to enforce https and a www prefix. However my rule doesn't fully work. Here is my rule:
<rewrite>
<rules>
<clear />
<rule name="Force https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Force www" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
<add input="{HTTP_HOST}" pattern="www.mydomain.co.uk" negate="true" />
</conditions>
<action type="Redirect" url="https://www.mydomain.co.uk/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
It works for redirecting http to https.
it works if I go to https://mydomain.co.uk (redirects to https://www.mydomain.co.uk)
however it DOES NOT work if I go to https://mydomain.co.uk/blah/whatever
Please can somebody advise? Thanks.
These are the rewrite rules that I use for that exact purpose. I've also added a rule to make the URL all lowercase and a rule to remove the trailing slash should one be present. This makes working with Analytics easier since it treats page.aspx and page.aspx/ as different url's. That is why I use ignoreCase=true because then it does not matter if someone uses upper case somewhere since it will be handled later on by the ToLowerCase rule
<rule name="ForceWWW" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yoursite.com" />
</conditions>
<action type="Redirect" url="https://www.yoursite.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="HTTPtoHTTPS" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="RemoveTrailingSlash">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="ToLowerCase">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
<conditions>
<add input="{URL}" pattern="WebResource.axd" negate="true" />
<add input="{URL}" pattern="ScriptResource.axd" negate="true" />
</conditions>
</rule>
Here is an example of such web.config -- it will force HTTPS for ALL resources (using 301 Permanent Redirect):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
<rewrite>
<rules>
<rule name="Redirects to www.example.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Source: https://stackoverflow.com/a/9823208/5740382
For more Details: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

url rewrite - error This webpage has a redirect loop

I would like to redirect all traffice coming from http://www.example.com to http://www.mysite.com/badreferer.aspx?bad=true
for this i tried to create a rule in web.config file of http://www.mysite.com in IIS7.
<rule name="bad referer" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="(.*)example(.*)" />
</conditions>
<action type="Redirect" url="/badreferer.aspx?bad=true" appendQueryString="false" />
</rule>
But having issue with redirect loop.
Please help.
Thanks.
Try this:
<rules>
<rule name="bad referer" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="http://www.example.com(.*)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.website.com/badreferer.aspx?bad=true" />
</rule>
</rules>
update:
<rule name="bad referer" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/badreferer\.aspx?bad=true" negate="true" />
<add input="{HTTP_REFERER}" pattern="^www.\example\.com.*" />
</conditions>
<action type="Redirect" url="/badreferer.aspx?bad=true" appendQueryString="false" />
</rule>

ASP.Net Redirect rule for all except certain URL

I have the following redirect rules one is for non www url (http://mysite.co.uk/....) and the other is for a www URL to redirect to http://www.mysite2.co.uk/...
Currently the rules are a catch all. I don't want the rules to execute if a certain URL is hit which contains "/mystring/mystring.aspx".
Can anyone help me write the rules for this?
<rule name="Canonical Host Name - mysite" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?:www|[^.]+\.)*mysite\.co.uk$" />
</conditions>
<action type="Redirect" url="http://www.mysite2.co.uk/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host - mysite 2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mysite\.co.uk$" />
</conditions>
<action type="Redirect" url="http://www.mysite2.co.uk/{R:1}" redirectType="Permanent" />
</rule>
You should be able to accomplish this by adding to the conditions, and using negate="true". Something like this:
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(?:www|[^.]+\.)*mysite\.co.uk$" />
<add input="{PATH_INFO}" pattern="mystring\/mystring\.aspx" negate="true" />
</conditions>

Resources