IIS url rewrite rule except home and some urls - asp.net-mvc

I want to remove www. from all requests and also redirect all requests from .info domain to .com domain except home page and some other urls like /usefulldoc/ but it's not working for the second rule.
This is my rules in web.config:
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect .info to .com with some Exceptins" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="example\.info$" negate="true" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="example\.info(.*)$" ignoreCase="true"/>
<add input="{REQUEST_URI}" pattern="^(.*)/usefulldoc(.*)$" negate="true" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" appendQueryString="true" />
</rule>
</rules>

Related

IIS using (URL Rewrite) to Redirect to another domain

trying to redirect from https://www.domain.info/cms-cp/login.html to https://www-server2.domain.info/cms-cp/login.html
by using the following code for (URL Rewrite) plugin, but unfortunately not working.
<rule name="Redirect To Web2 Server CMS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^(www.domain.info/cms-cp/login.html)$" />
</conditions>
<action type="Redirect" url="https://www-server2.domain.info/cms-cp/login.html" redirectType="Permanent" />
</rule>
<rule name="Redirect To Server2" patternSyntax="ECMAScript" enabled="true" stopProcessing="true">
<match url="login.html" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.domain\.info$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www-server2.domain.info/cms-cp/login.html" redirectType="Permanent" />
</rule>
{HTTPS} is just a bool value to check whether https has been enabled or not.
So you can apply the rule just like this
<rule name="Redirect To Web2 Server CMS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^on$" />
<add input="{HTTP_HOST}" pattern="www\.domain\.info" />
<add input="{URL}" pattern="^/cms-cp/login.html$" />
</conditions>
<action type="Redirect" url="https://www-server2.domain.info/cms-cp/login.html" />
</rule>

IIS Rewrite Removing H from HTTPS

I'm getting an issue with IIS Url Rewriting. After the first rule all seems well, but then the url being passed into the second rule doesn't have the 'h' on the schema. I've got a trace and the url that comes in is:
product/car.aspx
This then passes through the first rule below as:
https://fo.bar.com/product/car.aspx
But then the next rule is being passed in the url:
ttps://fo.bar.com/product/car.aspx
Any thoughts on this would be great. Below are my rules from the web.config
<system.webServer>
<rewrite>
<clear />
<rule name="HTTP to HTTPS" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost(:\d+)?$" negate="true" />
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^127\.0\.0\.1(:\d+)?$" negate="true" />
</conditions>
<action type="Rewrite" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<rule name="Remove incorrect first char from query string" enabled="false" stopProcessing="true">
<match url="(.*)"/>
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="^\&(.*)"/>
</conditions>
<action type="Rewrite" url="{R:0}?{C:1}" appendQueryString="false"/>
</rule>
..................
From the question it seems that all you want to do is redirect all HTTP traffic to HTTPS?
If so, you can simplify the rules to be:
<rule name="HTTP to HTTPS" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>

Azure app-service non-www to www redirect rule to ignore the cdn domain

The redirect rule below of non-www to www.sitedomain.com works, but the exemption clause for the CDN domain requests (add input="{HTTP_HOST}" pattern="cdnprefix.azureedge.net" negate="true") are ignored and the cdn requests redirect as well to www.sitedomain.com. Can you help me modify the rewrite section(s) to solve that?
<rule name="Redirect non-www to www.sitedomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<!-- domain is not canonical -->
<add input="{HTTP_HOST}" matchType="Pattern" ignoreCase="true" pattern="^sitedomain\.com$" />
<add input="{HTTP_HOST}" pattern="^www\.sitedomain\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="cdnprefix\.azureedge\.net" negate="true" />
</conditions>
<action type="Redirect" url="https://www.sitedomain.com{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
Together with the effective HTTP -> httpS redirect rule (split up to simplify solution for CDN exemption rule), it looks like this:
<rule name="Redirect to https">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="Off" ignoreCase="true" />
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect non-www to www.sitedomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<!-- domain is not canonical -->
<add input="{HTTP_HOST}" matchType="Pattern" ignoreCase="true" pattern="^sitedomain\.com$" />
<add input="{HTTP_HOST}" pattern="^www\.sitedomain\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="cdnprefix\.azureedge\.net" negate="true" />
</conditions>
<action type="Redirect" url="https://www.sitedomain.com{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
For canonical domain,you only need this rule
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www.\sitedomain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.sitedomain.com/{R:1}" />
</rule>
Then You can add the next section for CDN
<add input="{HTTP_HOST}" pattern="cdnprefix\.azureedge\.net" negate="true" />
Hope this works!

Too Many Redirects IIS URL Rewrite with multiple rules

I'm getting too many redirects with my two rules in IIS. One rule is to redirect all traffic to HTTPS, the other is to redirect to a 'not supported' page for browsers that don't fit my mapped criteria - this page is at the top level of the app. I am trying to exclude this 'NotSupported' page from the HTTPS rule as I believe this is what is causing all the redirects - but, it's not working... Anyone see what I'm doing wrong?
Thanks
<rewrite>
<rules>
<rule name="UserAgent MSIE Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="Chrome|MSIE 8.0|MSIE 7.0b|MSIE 7.0|MSIE 6.0b|MSIE 6.0|MSIE 5.5b1|MSIE 5.5|MSIE 5.0|MSIE 5.01|MSIE 4.0" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/NotSupported.html" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^/NotSupported\.html$" ignoreCase="true" />
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
EDIT:
This works (redirecting to Google)...
<rule name="UserAgent MSIE Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{REQUEST_URI}" negate="true" pattern="^/notsupported$" ignoreCase="true" />
<add input="{HTTP_USER_AGENT}" pattern="Chrome|MSIE 8.0|MSIE 7.0b|MSIE 7.0|MSIE 6.0b|MSIE 6.0|MSIE 5.5b1|MSIE 5.5|MSIE 5.0|MSIE 5.01|MSIE 4.0" />
</conditions>
<action type="Redirect" url="http://www.google.com/" appendQueryString="false" redirectType="Found" />
</rule>
But this doesn't - trying to use an internal address. How can I exclude this rule on that page so it stops the loop?
<rule name="UserAgent MSIE Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{REQUEST_URI}" negate="true" pattern="^/notsupported$" ignoreCase="true" />
<add input="{HTTP_USER_AGENT}" pattern="Chrome|MSIE 8.0|MSIE 7.0b|MSIE 7.0|MSIE 6.0b|MSIE 6.0|MSIE 5.5b1|MSIE 5.5|MSIE 5.0|MSIE 5.01|MSIE 4.0" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/notsupported.html" appendQueryString="false" redirectType="Found" />
</rule>
Any ideas?

Easy IIS 7.5 url rewrite rule (from non-www to www) gives redirect loop error in browser

This seems like it should be such a 101-level question but I cannot get it to work.
I very simply need to set up a url rewrite rule that redirects mydomain.com to www.mydomain.com. That's it. But no matter how I go at it, I get a URL redirect error in my browser.
Below are a few different rule definitions I have tried, one-by-one. As far as I can tell, any should work. But none have. I would be incredibly grateful for any insight.
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
<add input="{HTTP_HOST}" negate="true" pattern="^www.mydomain.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
<rule name="non-www to www" enabled="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" />
</rule>
In IIS, it's built in as a canonical host redirect. See below to 301 redirect any request from http://example.com to http://www.example.com
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>

Resources