IIS Redirect Rule to redirect one url to another - redirect

I Know this has been asked a lot but I just can't seem to figure out how to get this to work, even looking at other variables.
I need to redirect www.example.com/article-2 to www.example.com/article
<rule name="Redirect Dup Artivcle URL" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern=" https://www.example.com/article-2/" />
</conditions>
<action type="Redirect" url="https://www.example.com/article/" appendQueryString="false" />
</rule>
I feel like I may have the wrong URL matching pattern...

Its obviously that {HTTP_HOST} is not covering https://.
Please modify the rule like this
<rule name="Redirect Dup Artivcle URL" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="www.example.com/article-2/" />
<add input="{HTTPS}" pattern="^on$" />
</conditions>
<action type="Redirect" url="https://www.example.com/article/" appendQueryString="false" redirectType="Temporary" />
</rule>

Related

How to rediret all requests to another host in IIS

I want to redirect all requests from the old domain to a new domain. For example:
From: oldhost.com/app1/houses?city=springs
To: newhost.com/app1/houses?city=springs
I've tried using both the basic "HTTP Redirect" as far as "URL Rewrite" module with the same results. Redirect only works to a limited extent, as long as there is nothing past the first path segment:
oldhost.com/app1 - works
oldhost.com/app1/houses?city=springs - does not work
Here is the attempted URL Rewrite rule:
<rule name="rule1" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://newhost.com/{C:1}" logRewrittenUrl="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="oldhost.com(.*)" />
</conditions>
</rule>
You can try this rule:
<rule name="test">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^oldhost.com$" />
</conditions>
<action type="Redirect" url="https://newhost.com/app1{R:1}" />
</rule>
Got it to work with the following rule:
<rule name="my_redirect" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://newhost.com/{R:0}" logRewrittenUrl="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="oldhost.com" />
</conditions>
</rule>

Http tp Https URL re-write rule for app.hostname.com in IIS

I want my application app.hostname.com to redirected at https://app.hostname.com. I tried this one but it didn't work. it continuously redirecting to the same path after redirecting to https.
<rewrite>
<rules>
<rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
Please try to clear your browser cache and history.
try to use the below rule:
<rule name="HTTPS force" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
if there is any other rule please disable it.

IIS Redirect www to non-www and http to https: is it possible to do it with only one Redirect?

I need to avoid the double redirect I have after I created two IIS URL Rewrite rules to do:
1) Redirect www to non-www.
2) Redirect HTTP to HTTPS.
This is my code:
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="CanonicalHostNameRule1" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^mydomain\.com$" ignoreCase="true" negate="true" />
</conditions>
<action type="Redirect" url="https://ABC/{R:1}" />
</rule>
(ABC is mydomain.com name but I had to change it in order to be able to post the question)
The problem is that if I go to www it does two redirects, one from www to non-www and a second one from http to https.
I also tried having only one rules with both conditions but the result was not better.
Is there a way to only do one redirect?
This is the final configuration I used:
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://yourdomainname.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
It's only one rule that redirects to non-www and https url.
You should be able to with HTTP to HTTPS, the reverse is trickier depending on your IIS setup.
<rule name="HTTP to HTTPs and www to non-www " enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{SERVER_PORT}" pattern="^80$" />
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
Keep in mind, based on how you have sites structured in IIS (Bindings, sites, etc), it's all going to influence how your rewrite rules work and where your going to place them. At the app level or as a global rule.
Since I don't know how you have your bindings or IIS structured with other sites, It's impossible to write the URL Rewrite rule for you with 100% accuracy.
So you might need to do a little experimentation with the above syntax.
Of course. Just drop these rules instead: (no modification required)
<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>

web.config redirect multiple domains to one AND redirect HTTP to HTTPs

I am using web.config to redirect all HTTP traffic to a HTTPs website
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
This works just fine.
My client wants me to now redirect a whole load of other non HTTPs .com domains to this HTTPs .co.uk domain.
I found a redirect script:
<rule name="Redirect to www.MYDOMAIN.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www.)?MYOTHERDOMAIN1.(com|org)$" />
<add input="{HTTP_HOST}" pattern="^(www.)?MYOTHERDOMAIN2.(com|net|org)$" />
</conditions>
<action type="Redirect" url="http://www.MYDOMAIN.co.uk/{R:0}" />
</rule>
For the life of me I cannot seem to combine these script into one script that will pick any of his domains and cleanly point them in the direction of the HTTPs .co.uk domain.
Any ideas would be greatly appreciated as regular expressions are not my strong point.
Kind Regards
You can simply add two rules in the order they must process (with only the last rule set to stopProcessing).
<rules>
<clear />
<rule name="domain redirect" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?EXAMPLE2.(com|net)$" />
</conditions>
<action type="Redirect" url="http://www.EXAMPLE.net{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>

web.config redirect multiple domains to one

Hi i am trying to redirect my domain aliases to one domain.
I currently have this rule
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true"
pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}"
appendQueryString="true" redirectType="Permanent" />
</rule>
It works perfect when the alias doesnt have the www in front.. how do i say redirect all that is not equal to this domain
thanks
Try it. I'm not sure if it works or not, I'm not great in this subject but this has been sitting here for 4 months unanswered, so I thought I'd give it a wollop.
<rule name="Rewrite domain requests" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Rewrite" url="http://www.mydomain.com/url={R:1}" appendQueryString="true" />
</rule>
It's the pattern I'm unsure of. I think this says, match anything in the URL, whether with or without www, and any possible domain extension.
Add one rule for each domain. It keeps query string too:
Lloyd Zhang : http://forums.iis.net/t/1185885.aspx
<rule name="Domain Redirect" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://{C:1}mydomainalias.com/{R:1}" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mydomain\.com" />
</conditions>
</rule>
This will solve your problem.
You have to negate the main domain to avoid a redirect loop.
<rule name="Rewrite domain requests" stopProcessing="true" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?([.a-zA-Z0-9]+)$" />
<add input="{HTTP_HOST}" pattern="^www\.domain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>