IIS http to https rewrite rule and exclude IP address - iis

I wrote a http to https rewrite rule in IIS.
I want to exclude a http://XXX.XX.XXX.XX (one single IP address) from this.
Then I want to write another rule that rewrites those http://XXX.XX.XXX.XX and https://XXX.XX.XXX.XX to https://www.foo.com (my domain which I have ssl certificate for).
Here both rules:
<rule name="http to https redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{REQUEST_URI}" pattern="^/XXX.XX.XXX.XX$" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Redirect to my site">
<match url="XXX.XXX.XXX.XX" />
<conditions>
<add input="{QUERY_STRING}" pattern="XXX.XXX.XXX.XX" />
</conditions>
<action type="Rewrite" url="https:www.foo.com" />
</rule>
It does not work with the excluded pattern and I wonder if the second rule applies at all. Any advise is wellcome.

Related

Redirect the iis http address of two domains with the same reference to non-www https

I have two domains. Both read from the same source. That is, the path of their files on the server is the same with same web.config file. I want both of them to be redirect to the HTTPS path without WWW, regardless of the conditions that are called in the URL. Similar to below
HTTP to HTTPS
WWW to non-WWW
for example.com and example.co
I tried to use the below code:
<rewrite>
<rules>
<rule name="Redirect to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Special case for HTTPS with www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But this code cannot be considered for both domains com and co. Because it redirects only to the com domain.
Try splitting the two different domain names into two rules, and add the full hostname to your rules, which will trigger your rule for a specific domain only:
<rewrite>
<rules>
<rule name="Redirect example.com to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?example.com$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect example.co to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?example.co$" />
</conditions>
<action type="Redirect" url="https://example.co/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

Redirect from http to https on IIS 10.0

I have an aspx web app accessed via http://domain/web.aspx. This web app uses port 80 and http://domain:80/web.aspx works OK. I would like to redirect all calls to https://domain:82/web.aspx. I've tried using the rewrite rule
<rule name="HTTPS force" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*):80$" />
</conditions>
<action type="Redirect" url="https://localhost:82" appendQueryString="false" />
</rule>
in web.config in the same folder as web.aspx but this has no effect. What rewrite rule do I need to use?
You should use the following as input:
<rule name="HTTPS force" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
</rule>
Not sure if you want the :80 there in the URL? If so, it can be added to my example too. Taken from here: Web.config redirects with rewrite rules - https, www, and more.
Found this rule worked
<rule name="HTTPS force" enabled = "true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="http://domain/Web.aspx" />
</conditions>
<action type="Redirect" url="https://domain:82/Web.aspx"/>
</rule>

Create one rule to maintain multiple URLs

Windows Server 2012
I have 1 IIS container with 4 different URLs for one domain and 2 domains in total as below (set with the bindings)
www.site1.com
site1.com
www.site1.co.uk
site1.co.uk
www.site2.com
site2.com
www.site2.co.uk
site2.co.uk
www.site1.com - is the primary domain, meaning any combination of the URLs above must take the user to www.site1.com or www.site2.com
Below is the config i have for site 1 but site 2 is essentially the same. www.Site2.com not listed for brevity
<rule name="site1CoUk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="site1.co.uk" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
<rule name="site1WwwCoUk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.site1.co.uk" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
<rule name="site1Com" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="site1.com" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
Everything seems to work but is there a way to reduce the configuration so i can maintain it within one rule (or 2 rules if i add in the www.site2.com). So i did something like this
<rule name="site1CoUk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="site1.co.uk" />
<add input="{HTTP_HOST}" pattern="www.site1.co.uk" />
<add input="{HTTP_HOST}" pattern="site1.com" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
but this doesnt work as soon as i add the second HTTP_HOST pattern.
The main reason is as i add more URLS it would easier to maintain under a one rule rather than adding a new rule each time round? Eventually the site would become https so i assume i could change the URL.

URL Rewrite causing redirected you too many times

I am using IIS url rewrite rule to redirect from an IP Address to a domain name with the following rule.
<rule name="IPHit" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="10.32.1.132" />
</conditions>
<action type="Redirect" url="https://daart-qa.sandbox.aimsplatform.com/eds-daas/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
Unfortunately, this results in an infinite loop redirect when I go to https://daart-qa.sandbox.aimsplatform.com/eds-daas.
How can I redirect from the IP address without having an infinite loop on dns entry?
Your rule will redirect 10.32.1.132/eds-daas to https://daart-qa.sandbox.aimsplatform.com/eds-daas/eds-daas. Is that expected behavior?
May I know your only face this infinite loop when you access specific URL or all requests hitted by IP address. What loop URL did you see in web browser developer tool?
Post the symtptom of loop URL would help us find the root cause.
Or you need to redirect request 10.32.1.132/eds-daas to https://daart-qa.sandbox.aimsplatform.com/eds-daas? If so you may need two rules side by side.
<rule name="IPHit" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="10.32.1.132" />
<add input="{REQUEST_URI}" pattern="^/eds-daas(.*)" negate="true" />
</conditions>
<action type="Redirect" url="https://daart-qa.sandbox.aimsplatform.com/eds-daas/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
<rule name="IP-HIT2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="10.32.1.132" />
<add input="{REQUEST_URI}" pattern="^/eds-daas(.*)" />
</conditions>
<action type="Redirect" url="https://daart-qa.sandbox.aimsplatform.com/eds-daas{C:1}" />
</rule>

Rewrite to HTTPS with subdomain?

How can I setup web.config to handle the following redirects
http://example.com -> https://example.sub.com
http://www.example.com -> https://example.sub.com
http://example.sub.com -> https://example.sub.com
The part I am not sure about is matching the urls, for example,
http://<dynamic> to http://<dynamic>.sub.com
I would solve this with two rules. One will match when the hostname contains .sub.com and simply perform a plain redirect. The next will append .sub.com to the hostname.
These are based off #4 of https://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/#redirect-https
Finally, after you've proved that everything works as expected, changed the Found to Permanent in both rules.
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="\.sub\.com$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}.sub.com/{R:1}" redirectType="Found" />
</rule>

Resources