Redirecting from non-www. to www. in ASP Site

Fixing canonical issues in Microsoft IIS is no easy task. With the lack of native .htaccess support the only way you can do it is either via the IIS control panel or through the web.config file. Here i am going to show you the web.config method of making sure your URL’s are canonical.
In a .htaccess file, creating redirects is easy, however web.config is much harder because it’s all XML based.
If you already have a web.config file, back it up now! We’ll be editing it so we don’t want to lose the original. If you do and you get a step wrong, you could end up with a broken website.
Once you have done that, we need to miss some code out as it will already exist. This code is the basis of the web.config XML file so it’s included in all web.config files.
If you do not have a web.config file, simply create one and add this code to it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
This is the head of the file.
Here is the code...
<rewrite>
<rules>
<rule name="Cannonical Hostname" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>
<rule name="Default Document" stopProcessing="true">
<match url="(.*?)/?index.php$" />
<action type="Redirect" url="{R:1}/" />
</rule>
</rules>
</rewrite>
This code needs to go after the <system.webServer> block in the file. If this block exists already, you may need to dissect it a little. For example if the <rewrite> block already exists then you need to leave it off the code below and copy the code in between it on the existing file.
There is one last step before we can upload the file. If you made your file from scratch, then you’ll need to include the following snippet at the bottom. If it already existed, then the below code will already be there, so you can skip this step.
</system.webServer>
</configuration>
That’s it. Your web.config file is complete. All you need to do now is save the file as “web.config” and upload it to the root of your website.
I hope this has given you enough information to successfully make your URL’s canonical when using Microsoft ISS.
Thanks!
In a .htaccess file, creating redirects is easy, however web.config is much harder because it’s all XML based.
If you already have a web.config file, back it up now! We’ll be editing it so we don’t want to lose the original. If you do and you get a step wrong, you could end up with a broken website.
Once you have done that, we need to miss some code out as it will already exist. This code is the basis of the web.config XML file so it’s included in all web.config files.
If you do not have a web.config file, simply create one and add this code to it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
This is the head of the file.
Here is the code...
<rewrite>
<rules>
<rule name="Cannonical Hostname" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" />
</rule>
<rule name="Default Document" stopProcessing="true">
<match url="(.*?)/?index.php$" />
<action type="Redirect" url="{R:1}/" />
</rule>
</rules>
</rewrite>
This code needs to go after the <system.webServer> block in the file. If this block exists already, you may need to dissect it a little. For example if the <rewrite> block already exists then you need to leave it off the code below and copy the code in between it on the existing file.
There is one last step before we can upload the file. If you made your file from scratch, then you’ll need to include the following snippet at the bottom. If it already existed, then the below code will already be there, so you can skip this step.
</system.webServer>
</configuration>
That’s it. Your web.config file is complete. All you need to do now is save the file as “web.config” and upload it to the root of your website.
I hope this has given you enough information to successfully make your URL’s canonical when using Microsoft ISS.
Thanks!
