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