IIS rewrite module canonical URL with two domains - url

I have a webserver and I have two domains with name http://example.mx and http://example.com.mx both are pointing to the same webserver and root folder, I can use my website with both domains and for the moment I can use www. as well but this is what I want to remove, I did it and I got this web.config file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="CGI-exe" />
<add name="CGI-exe" path="*.exe" verb="*" modules="CgiModule" resourceType="File" requireAccess="Script" allowPathInfo="true" />
</handlers>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule2" enabled="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com\.mx$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com.mx/{R:1}" />
</rule>
<rule name="CanonicalHostNameRule1" enabled="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.mx$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.mx/{R:1}" />
</rule>
<rule name="http a https" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But the problem is the I go to http://example.mx it sends me to http://example.com.mx and it suppose to works like this:
If I go to http://www.example.mx redirects to http://example.mx
If I go to http://www.example.com.mx redirects to http://example.com.mx
but right now if I go to http://www.example.mx it redirects to http://example.com.mx and I dont want this, What I need to change in the pattern?

You should use this rules:
<rule name="CanonicalHostNameRule2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com\.mx$"/>
</conditions>
<action type="Redirect" url="http://example.com.mx/{R:1}" />
</rule>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.mx$" />
</conditions>
<action type="Redirect" url="http://example.mx/{R:1}" />
</rule>

Related

IIS url rewrite dont do anything

I would like to url redirect from:
https://media.domain.com/link to file.ext
to:
https://media.domain.com:8080/link to file.ext
I have made this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8080" enabled="true" stopProcessing="true">
<match url="(.*)/(.*)/(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
</conditions>
<action type="Redirect" url="http://{R:2}:8080/{R:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But when I enter https://media.domain.com/link to file.ext IIS just gives me a 404 error, and it does not redirect.
IIS simply ignores my url rewrite..
Any thoughts?
you could try below rule:
<rule name="domian rule" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^media.domain.com$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://media.domain.com:8080/{R:1}" />
</rule>

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>

IIS url rewrite rule except home and some urls

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>

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!

Resources