Multiple URL Rewrite rules getting conflicted - iis

I have the following situation with url rewrite rules which are getting conflicted with each other:
Rule 1: I need to redirect my domain to https
Rule 2: I need to redirect www.mydomain.com --> https://mydomain.com
Rule 3: I need both www.mydomain.com and mydomain.com to redirect to https://mydomain.com/myfolder, but if I have mydomain.com/mysecondfolder should only redirct to https://mydomain.com/mysecondfolder
what I was able to achieve is everything but redirecting www.mydomain.com to https://mydomain.com (just because it is being conflicted with another rules, if alone it is working).
My rules are:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="redirect to myfolder" enabled="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/myfolder" />
</rule>

I was able to solve this using the below rules:
<rewrite>
<rules>
<rule name="Canonical Host Name" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^myapp\.com$" />
</conditions>
<action type="Redirect" url="http://myapp.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="redirect to items" enabled="false">
<match url="^$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="/items" />
</rule>
</rules>
</rewrite>

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>

web.config redirect to www and redirect old pages to subdomain

I hope you can help me.
Originally I set up a rule to redirect all non-www traffic to the www site like this:
<rewrite>
<rules>
<rule name="Redirect https://example.com to https://www.example.com HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$"></add>
<add input="{HTTPS}" pattern="on"></add>
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
</rule>
</rules>
</rewrite>
Later on down the line we created a new site and moved the old site to https://business.example.com
When this happened, I was asked to create 301 redirects for the old blog posts to the new sub domain.
I created these rules:
<rewrite>
<rules>
<rule name="301 Redirect 1" stopProcessing="true">
<match url="live-better" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 2" stopProcessing="true">
<match url="a-zero-sum-game-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/a-zero-sum-game-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 3" stopProcessing="true">
<match url="buying-online-doesnt-mean-shopping-online" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/buying-online-doesnt-mean-shopping-online" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 4" stopProcessing="true">
<match url="the-truth-about-experiential-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/the-truth-about-experiential-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 5" stopProcessing="true">
<match url="winning-the-battle-for-brand-engagement" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/winning-the-battle-for-brand-engagement" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 6" stopProcessing="true">
<match url="experiential-technology-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/experiential-technology-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 7" stopProcessing="true">
<match url="what-retailers-have-forgotten" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/what-retailers-have-forgotten" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
When I initally tested this, it seemed to be working. But upon further investigation only the live-better path is redirected properly.
I changed my 301 redirects to this:
<rule name="301 redirects" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="ON" />
<add input="{PATH_INFO}" pattern="^/live-better" />
<add input="{PATH_INFO}" pattern="^/live-better/a-zero-sum-game-in-retail" />
<add input="{PATH_INFO}" pattern="^/live-better/buying-online-doesnt-mean-shopping-online" />
<add input="{PATH_INFO}" pattern="^/live-better/the-truth-about-experiential-retail" />
<add input="{PATH_INFO}" pattern="^/live-better/winning-the-battle-for-brand-engagement" />
<add input="{PATH_INFO}" pattern="^/live-better/experiential-technology-in-retail" />
<add input="{PATH_INFO}" pattern="^/live-better/what-retailers-have-forgotten" />
</conditions>
<action type="Redirect" url="https://business.example.com/{R:1}" redirectType="Permanent" />
</rule>
But it seems that now everything is redirected to https://business.example.com.
I thought it might be the original rule:
<rule name="Redirect https://example.com to https://www.example.com HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$"></add>
<add input="{HTTPS}" pattern="on"></add>
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" appendQueryString="true"></action>
</rule>
If I am not mistaken, it will try to redirect https://anything.example.com to https://www.example.com (which might be why the redirects are not working).
Can anyone with more experience with this tell me what I am doing wrong?
I need the redirect from non-www to www, but at the same time I need the 301 redirects for the old blog posts.
Any help is massively appreciated.
I think I solved it.
It appears that order matters, so I changed the order so that "live-better" was last. I updated the non-www to www by adding the scheme like this:
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^situlive.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.situlive.com/{R:1}" />
</rule>
(thanks to this post: web.config redirect non-www to www)
so the full section now looks like this:
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" />
</rule>
<rule name="301 Redirect 1" stopProcessing="true">
<match url="a-zero-sum-game-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/a-zero-sum-game-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 2" stopProcessing="true">
<match url="buying-online-doesnt-mean-shopping-online" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/buying-online-doesnt-mean-shopping-online" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 3" stopProcessing="true">
<match url="the-truth-about-experiential-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/the-truth-about-experiential-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 4" stopProcessing="true">
<match url="winning-the-battle-for-brand-engagement" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/winning-the-battle-for-brand-engagement" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 5" stopProcessing="true">
<match url="experiential-technology-in-retail" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/experiential-technology-in-retail" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 6" stopProcessing="true">
<match url="what-retailers-have-forgotten" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better/what-retailers-have-forgotten" redirectType="Permanent" />
</rule>
<rule name="301 Redirect 7" stopProcessing="true">
<match url="live-better" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="https://business.example.com/live-better" redirectType="Permanent" />
</rule>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^main.js\/debug[\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="main.js"/>
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

iis rewrite url for https://example.com/default.aspx/default.aspx to https://example.com

I have tried all kinds of rewrite URL's and can't get IIS to send the URL of https://example.com/default.aspx/default.aspx to https://example.com/default.aspx
Google has indexed the site with the wrong URL when Bing got it right (go figure). Any help would be much appreciated. All my traffic is going to the wrong url (https://example.com/default.aspx/default.aspx).
<rewrite>
<rules>
<rule name="Redirect www.xxx.com to xxx.com" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^https://example.com/default.aspx/default.aspx" />
</conditions>
<action type="Redirect" url="default.aspx" />
</rule>
<rule name="redirect two character to default" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/[a-z]{2}(/)?$" />
</conditions>
<action type="Redirect" url="default.aspx" appendQueryString="false" />
</rule>
</rules>
</rewrite>
There is some issue in your rule. {HTTP_HOST} only matches the hostname which is www.example.com it will not match the whole URL.
You could try below rule:
<rule name="Redirect www.xxx.com to xxx.com" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" />
<add input="{HTTPS}" pattern="on" />
<add input="{REQUEST_URI}" pattern="default/default|default.aspx/default.aspx" />
</conditions>
<action type="Redirect" url="default.aspx" />
</rule>

Redirect https www to https non-www in web api 2 azure website

In a Azure portal app, I configured traffic to redirect to https but https://www won't redirect to https://
Redirection from http://, http://www both work correctly.
Those are rules I have in web.config in azure app.
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="NonWwwRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" />
</rule>
<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(signalr)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(token)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
How can I achieve the needed redirect?
The problem was with DNS records. A record with host www was pointed to different IP. Changed it to same IP as # host, that solved the problem
How about:
<rewrite>
<rules>
<rule name="Redirect www OR non-https to https://" enabled="true" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAny>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
You could refer to the code as below:
<rule name="NonWwwRedirect" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:0}" redirectType="Permanent" />
</rule>
Note: Make sure the example.com hostname you have assigned to Site, so that you could reach it successfully.

IIS RewriteRules

I have a RewriteRule that prepends "www" to the URL, granted that it does not already exist.
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
The trouble is, I have another inbound domain (www.example2.com) for which I do not want this rewrite rule to apply. I would think that the following would work fine.
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
<add input="{HTTP_HOST}" pattern="www.example2.com" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
But it just seems to be ignored. Any suggestions? Thanks!
You can do it with this rule. It will add www, only for domain example.com and will ignore all other domains:
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
Victor got me most of the way there. Thanks!
<rule name="Add www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
patternSyntax has to be the default regular expression value. That means the URL pattern had to be altered, and the backtrack expression is zero-based.

Resources