web.config redirect multiple domains to one - web-config

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>

Related

IIS URL Rewrite - Change Beginning of URL

I would like to replace
https://web1.domain.org/cwweb/LauncherInterface.aspx?host=https://web1.domain.org/
with
https://web2.domain.org/cwweb/LauncherInterface.aspx?host=https://web2.domain.org/
and keep everything else after it.
e.g. "https://web2.domain.org/xxxxx/LauncherInterface.aspx?host=https://web2.domain.org/xxxxx&thisid=zzzz"
This is what I have tried:
<rewrite>
<rules>
<rule name="redirect app" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="web1.domain.org" />
</conditions>
<action type="Redirect" url="https://web2.domain.org/cwweb/LauncherInterface.aspx?host=https://web2.domain.org/{R:1}" />
</rule>
</rules>
</rewrite>
This is what it displays: ""https://web2.domain.org/xxxxx/LauncherInterface.aspx?host=https://web1.domain.org/xxxxx&thisid=zzzz"
The second domain name doesn't change.
I pretty much a noob with this. Any assistance would be appreciated.
You can try this rule:
<rule name="test" stopProcessing="true">
<match url="^cwweb/LauncherInterface.aspx$" />
<conditions>
<add input="{HTTP_HOST}" pattern="web1.domain.org" />
<add input="{QUERY_STRING}" pattern="(.*)web1(\.domain\.org.*)" />
</conditions>
<action type="Redirect" url="https://web2.domain.org/{R:0}?{C:1}web2{C:2}" appendQueryString="false" />
</rule>

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>

Syntax in IIS to use re-write subdomain as path of {HTTP_HOST}

Suppose I have a site: https://sub.domain.example, what is the proper syntax to get this to become https://domain.example/sub
The need is regardless of the sub and the Toplevel domain (e.g., .com,.net,.org) to have the redirect push the user to https://domain.example/sub
Here is what I have tried but no go--
<rules>
<rule name="SubDomain to Subdirectory">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(\w+)\.(.*)$" />
</conditions>
<action type="Rewrite" url="https://domain.example/{C:1}" />
</rule>
</rules>
You could try the below rule to achieve your requiremnet:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="on" />
<add input="{HTTP_HOST}" pattern="([_0-9a-z-]+)\.(.*)" />
</conditions>
<action type="Redirect" url="https://domain.example/{C:1}" />
</rule>

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>