Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft rewriting module - Force www on url Or remove www from url

I have a shared hosting plan with Windows Server 2008 and IIS7.5, and there is Microsoft rewriting module installed and enabled.

<rewrite>
    <rules>
        <rule name="myRule" patternSyntax="Wildcard">
            <!--Rewriting code-->
        </rule>
    </rules>
</rewrite>

So, how to redirect mydomain.com/everywhere-in-site/my-page.html to www.mydomain.com/everywhere-in-site/my-page.html with Microsoft rewriting module?

And what if I want to redirect www.mydomain.com/everywhere-in-site/my-page.html to mydomain.com/everywhere-in-site/my-page.html ?

like image 876
Mahdi Ghiasi Avatar asked Apr 14 '12 12:04

Mahdi Ghiasi


People also ask

Does the URL in the browser change when a rewrite happens?

A rewrite is a server-side rewrite of the URL before it's fully processed by IIS. This will not change what you see in the browser because the changes are hidden from the user. Useful for search engine optimization by causing the search engine to update the URL.

Is URL rewriting safe?

While the process of URL rewriting may be understood by IT professionals, many users may be under the impression that any 'Safe' link is indeed safe - although this is often not the case. Thus, URL rewriting can actually have the side effect of increasing the likelihood of users clicking on malicious links.

What is the advantage of URL rewriting?

Specifically, URL Rewriting can often make it easier to embed common keywords into the URLs of the pages on your sites, which can often increase the chance of someone clicking your link.


1 Answers

To remove the www from a domain and redirect to a "naked domain" you could di it like in the following code snippet:

<rewrite>
  <rules>
    <rule name="Remove WWW prefix" stopProcessing="true">
      <match url="(.*)" ignoreCase="true" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.yourdomain\.com$" />
      </conditions>
      <action type="Redirect" url="http://yourdomain.com/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

And the other way around (if you prefer that) to redirect a non-www to one with www:

<rewrite>
  <rules>
    <rule name="Add WWW prefix" stopProcessing="true">
      <match url="(.*)" ignoreCase="true" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^yourdomain\.com$" />
      </conditions>
      <action type="Redirect" url="http://www.yourdomain.com/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

The redirectType="Permanent" is of course optional but for SEO and most scenarios I would recommend it.

Please see also these SO questions/answers:

  • IIS7 URL Rewrite - Add "www" prefix
  • Forwarding http://mydomain.com/ctrlr/act/val to http://WWW.mydomain.com/ctrlr/act/val
  • Proper method to remove www from address using IIS URL Rewrite
like image 165
Martin Buberl Avatar answered Nov 16 '22 02:11

Martin Buberl