IIS 301 redirect partial match - iis

I have this rule:
<rule name="301 Redirect 1" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^/live-better/.*" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/business/live-better" redirectType="Permanent" />
</rule>
Which I want to work for the following:
www.test.com/live-better
www.test.com/live-better/page-one
www.test.com/live-better/page-two
and I want them to all redirect to the counterpart:
https://www.example.com/business/live-better
https://www.example.com/business/live-better/page-one
https://www.example.com/business/live-better/page-two
But my rule does not appear to work.
Does anyone know how I can get it to work?

you could try below rule:
<rule name="301 Redirect 1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^/live-better/(.*)" />
</conditions>
<action type="Redirect" url="https://www.example.com/business/live-better/{C:1}" redirectType="Permanent" />
</rule>

Related

IIS Redirect olddomain.com/en-us/ABC to newdomain.com/ABC

I'd like to put a redirect in web.config from olddomain.com/en-us/abc/blahblah to newdomain.com/abc/blahblah
Meaning in addition to the change of domain, the new URL doesn't have /en-us/, and everything else remain the same.
I picked up from here and there and came up with the following code, but it doesn't work. Could you help?
<rule name="Redirect New Site" enabled="true" stopProcessing="true">
<match url="^/en-us/(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{PATH_INFO}" pattern="^/abc/(.*)" />
<add input="{HTTP_HOST}" pattern="^olddomain.com/$" />
</conditions>
<action type="Redirect" url="https://newdomain.com/{R:1}" redirectType="Permanent" />
</rule>
For this requirement, I test it in my side. You can use the <rule> like below:
<rule name="redirectRule1" enabled="true" stopProcessing="true">
<match url="^en-us/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^olddomain$" />
</conditions>
<action type="Redirect" url="http://newdomain.com/{R:1}" />
</rule>
Here is the rule and test result in my side. The <rule> in my web.config is:
<rule name="redirectRule1" enabled="true" stopProcessing="true">
<match url="^en-us/(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^localhost:49293$" />
</conditions>
<action type="Redirect" url="http://newdomain.com/{R:1}" />
</rule>
Please note my old domain is localhost:49293.
Then when I request the url http://localhost:49293/en-us/Upload, it will redirect to new url http://newdomain.com/Upload(without en-us).
============================Update============================
<rule name="redirectRule1" enabled="true" stopProcessing="true">
<match url="^en-us/abc/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^olddomain$" />
</conditions>
<action type="Redirect" url="http://newdomain.com/abc/{R:1}" />
</rule>

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 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.

Multiple URL Rewrite rules getting conflicted

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>

IIS7 URL Rewrite module rule help

I'm trying to turn off HTTPS when someone hits the root of my site...
I want
https://www.domain.com/
to redirect to
http://www.domain.com/
but I also want..
https://www.domain.com/secure.aspx
or any other named page not to redirect.
Here's what I have so far but I can't get it to work. I tried a thousand ways, and read all the IIS documentation and examples on this, but I can't come up with the magic formula.
<rule name="Redirect Root Only to Non HTTP" stopProcessing="true">
<match url="\.com/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://www.domain.com" appendQueryString="false" redirectType="Found" />
</rule>
I think this is your answer:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^.*\.aspx$" ignoreCase="true" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^$" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/" redirectType="Permanent" />
</rule>
You were close .. except the actual pattern (which matches URL path part only and not domain name). The proper pattern is ^$ which means empty string which will ONLY match domain root hit e.g. https://www.domain.com/.
The full rule will be:
<rule name="Redirect Root to HTTP" stopProcessing="true">
<match url="^$"/>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="on"/>
</conditions>
<action type="Redirect" url="http://www.domain.com/" redirectType="Permanent"/>
</rule>

Resources