Start a conversation

How to redirect domain.com to www.domain.tld in web.config

<pre class="code"><span>On Windows Server 2008 IIS 7.5</span><br /><br />You can add rewrite rule in web.config file to redirect domain.com to www.domain.com<br /><br /><strong>Option 1:</strong><br /><br /><code><span class="tag"><system.webserver></span><span class="pln"><br /></span><span class="tag"><rewrite></span><span class="pln"><br />Â  Â </span><span class="tag"><rules></span><span class="pln"><br />Â  Â  Â </span><span class="tag"><rule</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"Canonical host name"</span><span class="tag">></span><span class="pln"><br />Â  Â  Â  Â </span><span class="tag"><match</span><span class="pln"> </span><span class="atn">url</span><span class="pun">=</span><span class="atv">"^(.*)"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"><br />Â  Â  Â  Â  Â </span><span class="tag"><conditions></span><span class="pln"><br />Â  Â  Â  Â  Â  Â </span><span class="tag"><add</span><span class="pln"> </span><span class="atn">input</span><span class="pun">=</span><span class="atv">"{HTTP_HOST}"</span><span class="pln"> </span><span class="atn">pattern</span><span class="pun">=</span><span class="atv">"^www\.domain\.com$"</span><span class="pln"> </span><span class="atn">negate</span><span class="pun">=</span><span class="atv">"true"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"><br />Â  Â  Â  Â  Â </span><span class="tag"></conditions></span><span class="pln"><br />Â  Â  Â  Â </span><span class="tag"><action</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"Redirect"</span><span class="pln"> </span><span class="atn">url</span><span class="pun">=</span><span class="atv">"http://www.domain.com/{R:1}"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"><br />Â  Â  Â  </span><span class="tag"></rule></span><span class="pln"><br />Â  Â </span><span class="tag"></rules></span><span class="pln"><br /></span><span class="tag"></rewrite></span><span class="pln"><br /></span><span class="tag"></system.webServer></span><span class="pln"><br /><br /><strong>Option 2:</strong><br /></span></code><br /><system.webserver> <rewrite> <rules> <clear /> <rule name=<span class="code_str">"WWW Rewrite"</span> enabled=<span class="code_str">"true"</span>> <match url=<span class="code_str">"(.*)"</span> /> <conditions> <add input=<span class="code_str">"{HTTP_HOST}"</span> negate=<span class="code_str">"true"</span> pattern=<span class="code_str">"^www\.([.a-zA-Z0-9]+)$"</span> /> </conditions> <action type=<span class="code_str">"Redirect"</span> url=<span class="code_str">"http://www.{HTTP_HOST}/{R:0}"</span> appendQueryString=<span class="code_str">"true"</span> redirectType=<span class="code_str">"Permanent"</span> /> </rule> </rules> </rewrite> <system.webserver></pre>

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Senior Engineer Jessie

  2. Posted
  3. Updated

Comments