Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.config redirect non-www to www

I need to redirect non-www urls to www url for both http and https urls. I tried following rules in web.config.

<rule name="Redirect to WWW" stopProcessing="true"> <match url=".*" /> <conditions>     <add input="{HTTP_HOST}" pattern="^domain.com$" /> </conditions> <action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" /> 

<rule name="Redirect to WWW https" stopProcessing="true"> <match url=".*" /> <conditions>     <add input="{HTTPS}" pattern="^domain.com$" /> </conditions> <action type="Redirect" url="https://www.domain.com/{R:0}" redirectType="Permanent" /> 

It works perfectly for non-ssl url but in case of ssl it redirect from https://domain.com to http://www.domain.com

Please help me to correct my rules.

like image 231
Anand Avatar asked Jul 18 '13 04:07

Anand


People also ask

How do I redirect www to non-www in web config?

Use IIS rewrite rule to redirect (301) all www requests to non-www. Code first, talks later. Replace the “yourdomain” with your domain name and add it under the system. webServer section in the Web.

How do I redirect a domain without www to www?

Click on the Redirects icon under the Domains area of your cPanel home page. Select your domain name from the drop down menu on the next line. In the redirects to text box, type in the full URL of your domain, without the www (e.g. http://yourdomain.com). Select the radio button next to Only redirect with www.


2 Answers

For a safer rule that works for both Match Any and Match All situations, you can use the Rewrite Map solution. It’s a perfectly good solution with the only drawback being the ever so slight extra effort to set it up since you need to create a rewrite map before you create the rule. In other words, if you choose to use this as your sole method of handling the protocol, you’ll be safe.

You can create a Rewrite Map called MapProtocol, you can use {MapProtocol:{HTTPS}} for the protocol within any rule action.

<rewrite>   <rules>     <rule name="Redirect to www" stopProcessing="true">       <match url="(.*)" />       <conditions trackAllCaptures="false">         <add input="{HTTP_HOST}" pattern="^domain.com$" />       </conditions>       <action type="Redirect"          url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />     </rule>   </rules>   <rewriteMaps>     <rewriteMap name="MapProtocol">       <add key="on" value="https" />       <add key="off" value="http" />     </rewriteMap>   </rewriteMaps> </rewrite> 

Reference

like image 115
Satpal Avatar answered Oct 02 '22 15:10

Satpal


Other answers here are unnecessary long, here's the rule i use (just copy and paste) :

<rule name="ensurewww" stopProcessing="true">   <match url=".*" />   <conditions>     <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />   </conditions>   <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" /> </rule> 

This will redirect to the www version while preserving the HTTP and HTTPS protocol

Hope this helps.

like image 28
Chtiwi Malek Avatar answered Oct 02 '22 14:10

Chtiwi Malek