Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS7 URL Rewrite - Add "www" prefix

How to force example.com to be redirected to www.example.com with URL rewriting in IIS7? What kind of rule should go into the web.config? Thanks.

like image 808
niaher Avatar asked Feb 05 '10 12:02

niaher


People also ask

What is a URL Rewrite rule?

A rewrite rule defines the logic of what to compare or match the request URL with, and what to do if the comparison is successful. Rewrite rules consists of the following parts: Pattern – The rule pattern is used to specify either the regular expression or a wildcard pattern that is used to match URL strings.

What is rewrite tag in Web config?

Rewrite rules is a powerful feature in IIS. Common tasks like redirecting www to non-www (or the other way around), implementing canonical URLs, redirecting to HTTPS, and similar tasks are documented right there in your Web. config file.


3 Answers

To make it more generic you can use following URL Rewrite rule which working for any domain:

<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer>     <rewrite>         <rules>               <rule name="Add WWW" stopProcessing="true">               <match url="^(.*)$" />               <conditions>                  <add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />               </conditions>               <action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />            </rule>         </rules>     </rewrite> </system.webServer> 

like image 182
Atashbahar Avatar answered Sep 20 '22 09:09

Atashbahar


This is Microsoft's sample for URL Rewrite Module 2.0 that redirects *.fabrikam.com to www.fabrikam.com

<?xml version="1.0" encoding="UTF-8"?> <configuration>     <system.webServer>         <rewrite>             <rules>                 <rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">                     <match url="*" />                     <conditions>                         <add input="{HTTP_HOST}" pattern="www.fabrikam.com" negate="true" />                     </conditions>                     <action type="Redirect" url="http://www.fabrikam.com/{R:1}" />                 </rule>             </rules>         </rewrite>     </system.webServer> </configuration> 
like image 20
orad Avatar answered Sep 18 '22 09:09

orad


Not sure about the best possible way to do this, but I have a site with all old domains / subdomains running this web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Transfer" stopProcessing="true">
                    <match url=".*" />
                    <action type="Redirect" url="http://www.targetsite.com/{R:0}" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Seems to get the job done.

like image 38
Sciolist Avatar answered Sep 18 '22 09:09

Sciolist