IIS7 URL Rewrite - Add "www" prefix

niaher picture niaher · Feb 5, 2010 · Viewed 36k times · Source

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.

Answer

Atashbahar picture Atashbahar · Aug 12, 2015

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>