iis url redirect http to non www https - redirect

i need to redirect from
www.domain.de to https://domain.de
-works
http://www.domain.de to https://domain.de
-works
http://domain.de to https://domain.de
-does not work
rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

I think this will work for you, the search pattern has the optional www and redirects using the back reference C:2, the rule has a condition to only run against non https.
This is the pattern:
"^(www\.)?(.*)$"
where:
{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de
Here's the rule in full:
<rewrite>
<rules>
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

The accepted answer doesn't handle the special case https://www.domain.de.
These rules do the complete job:
<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>

If you want to redirect www to non www:
Add DNS entry for www.yourdomain.com to refer to your server's public IP
Then you need to Edit Bindings of your website and "Add Binding" www.yourdomain.com
Add a rewrite rule to your website using iis:
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
Reference: http://madskristensen.net/post/url-rewrite-and-the-www-subdomain

If you want something more flexible than for your three examples, change your HTTP_HOST pattern to : \w+\.\w+$. That would work for all three examples plus anything else, like subdomain.abcdef.domain.de.
If you use this regex either encase it in parenthesis or change C:1 to C:0 in your action.

This is what worked for me:
<rule name="NameRule1" stopProcessing="true" enabled="true" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" />
</rule>
I got it from: https://medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704

Redirecting https://www.example.com to https://example.com I had to do 2 steps
Select the website, click "URL Rewrite" -> Click "Add Rule" -> Choose "Cononcial domain name" -> select your domain (without the www) -> make sure rule is at the top -> highlight the rule and click "Edit" -> and update "Redirect URL" at the end of the rule to include the 's' - https://example.com/{R:1}
I had to also create a new website with the domain www.example.com, new app pool and select the SSL cert and point it to a folder containing the following (for some reason StackOverflow wont display the code I pasted below for the rewrite rule but just google IIS rewrite rule)
After doing both steps only then does it work as desired

Tried many of solutions but below works for me:
Right click on Domain and Add a Site bindings, add one more domain with www:
2. Restart services. it should work.

These rewrite rules matches the following URL's:
www.example.com
http://www.example.com
https://www.example.com
They will all redirect to: https://example.com
These rewrite rules may redirect twice because of the separate rules. (I'm a newbie with regex)
<configuration>
<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="WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

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.

How to redirect all traffic to https & non-www in web.config?

I'm using an SSL certificate and IIS. In my web.config I want to redirect all website traffic in the following order:
all http --> https
all www --> non-www
You want to use the redirect module in order to do this. This intercepts the request as it's coming in to the server and changes it according to your instructions.
Here is an action to redirect all to https:
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
and here is one to redirect www to non-www:
<conditions> <add input=”{HTTP_HOST}” pattern=”^example\.com$” negate=”true” /> </conditions> <action type=”Redirect” url=”http://example.com/{R:1}” />
There are plenty of articles around the web and StackOverflow when you include both "web.config" and "redirect" in your search.
Make sure that when you set up the rules you only use stopProcessing="true" on the final rule. If that appears on the first rule then the second rule will never execute.
i find the answer
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="NonWwwRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.yoursit\.com$" />
</conditions>
<action type="Redirect" url="http://yoursite.com/{R:1}" />
</rule>
<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>
</rules>
</rewrite>
<system.webServer />

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

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>