URLRewrite to add www for http and https - asp.net

I need url-rewrite (or any other method) do the following:
http://domain.com > http://www.domain.com
https://domain.com > https://www.domain.com
I've seen a lot of posts to redirect to www and a lot to force https but none of them that does both.
Thanks

this should help:
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.+)://" />
<add input="{HTTP_HOST}" pattern="^domain\.com$" />
</conditions>
<action type="Redirect" url="{C:1}://www.domain.com/{R:1}" />
</rule>

this rule i use is applying WWW for both http and https (just copy and paste) :
<rule name="ensure www" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
hope this helps.

Related

How can I redirect an IIS website to use www

As a user trying to access https://www.qa1.samplewebsite.org
I want to be redirected to https://qa1.samplewebsite.org
The site without www works just fine but when www is appended, it says 'Can't reach the page'
Used url rewrite 2 rules but didn't work
The web.config section is as follows:
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If you want to redirect www to non www, you can refer to this rule:
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:0}" redirectType="Permanent" />
</rule>

url redirection in IIS not working for non existing pages

I had a page http://domain1.com/blog.aspx.
This page I have deleted and created a website with a new domain just for this page.
like below http://domain2.com/blog.aspx
Then I have added a rule in domain1.com web.config like below
<rewrite>
<rules>
<rule name="Redirect blog" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1.com/blog.aspx$" />
</conditions>
<action type="Redirect" url="http://domain2.com/blog.aspx" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But I get 404 error while visiting http://domain1.com/blogpage.aspx
How can I fix this issue?
A few issues...
{HTTP_HOST} = The host name which is domain1.com, so it will never match your pattern
Also you can do this pattern="^domain1.com/blog.aspx$" the . need to be escaped.
Now you have to match also on the request_uri to capture the page. Below should work. Providing you are re-directing domain1.com/blogpage.aspx to domain2.com/blog.aspx
<rule name="Redirect blog" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" >
<add input="{HTTP_HOST}" pattern="^domain1\.com$" />
<add input="{REQUEST_URI}" pattern="blogpage\.aspx$" />
</conditions>
<action type="Redirect" url="http://domain2.com/blog.aspx" redirectType="Permanent" />
</rule>
</rules>

Web.config URL Rewrites - HTTPS and Non-WWW

I need to have both https and non-www rewrites, while also NOT HARDCODING the domain, since we have numerous servers. This needs to be in the web.config, not in IIS.
I've read numerous articles:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
how to set asp.net web.config rewrite http to https and www to non-www
The https rewrite works, the non-www does not.
<rule name="Redirect to HTTPS" 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="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<!--<add input="{CACHE_URL}" pattern="*://www.*" />-->
<!--<add input="{HTTP_HOST}" pattern="*://www.*" />-->
<add input="{HTTP_HOST}" pattern="^.*www.*" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
// i've also tried
// url="{C:2}/{R:1}"
// url="{C:1}/{C:2}"
</rule>
I tested the regex for ^.*www.* on a regex tester and it was matching www.testing.com but not testing.com - so I would assume the pattern would catch it.
I need the URLs to redirect from:
testing.com ---> https://testing.com
www.testing.com ---> https://testing.com
www.testing.com/xyz/ ---> https://testing.com/xyz/
Was my own issue - there was no DNS for the www, therefore the redirect wouldn't resolve on it's own.
Code used:
<rule name="Redirect to HTTPS" 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="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>
Add a "Canonical domain name" rule.
The soulution Rob found works fine as well. Mine seems to be a bit easier IMHO.

IIS Re-write http to https with subfolders

As simple as it sounds and as much as I've searched, I'm not able to get
http://example.com/subfolder to redirect to https://example.com/subfolder using IIS rewrite.
Note: This is only when "http" is explicitly stated in the browser.
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />enter code here
Thanks!
The problem with your rule is that REQUEST_URI in your redirect action is the whole URI as requested. What you want is the result of the rule match. R1 will give you this. This answer gives a good explanation of the rule back-references. A working rule can be constructed like this:
<rewrite>
<rules>
<rule name="HTTP to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>

Proper method to remove www from address using IIS URL Rewrite

What is the optimal way to remove the www subdomain from a url using IIS URL Rewrite?
If you want it to work with any hostname (not hardcoding it into the rule), you'd want to do something like this:
<rule name="Remove www" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
in the redirect action, the {C:1} contains the second capturing group in the condition, whereas the {R:0} contains whatever was in the rule (the path). appendQueryString="true" will also append any querystring to the redirect (if present). Keep in mind though, that any url hashes, if present, will be lost in the process since those don't get passed to the server.
IIS does it automatically for you:
Select site > URL rewrite > new rule > Canonical Host Name :)
The following one should work :
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.example.com{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
To do a redirect that will work for both http and https the following can be used
<rewrite>
<rules>
<rule name="Lose the www" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.*)$"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="{SchemeMap:{HTTPS}}://{C:1}/{R:1}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="SchemeMap">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

Resources