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.example/everywhere-in-site/my-page.html to www.mydomain.example/everywhere-in-site/my-page.html with Microsoft rewriting module?
And what if I want to redirect www.mydomain.example/everywhere-in-site/my-page.html to mydomain.example/everywhere-in-site/my-page.html?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.example$" />
</conditions>
<action type="Redirect" url="http://yourdomain.example/{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.example$" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.example/{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 Stack Overflow questions/answers:
- IIS7 URL Rewrite – Add “www” prefix
- Forwarding http://mydomain.example/ctrlr/act/val to http://WWW.mydomain.example/ctrlr/act/val
- Proper method to remove www from address using IIS URL Rewrite
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0