I need to resolve canonical URLs :
non-www version should be redirected (301) to www (all pages)
IP address should be redirected to www.mywebsite.com
www.mywebsite.com/index.cfm should be redirected to www.mywebsite.com
I would really appreciate your help on this one as the following code (.htaccess file) redirects only non-www version to www
I'm not sure what to change/add to include all of the above rules.
RewriteEngine on
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
Technically its not built in but you can grab it from here: http://www.iis.net/download/URLRewrite
They have a rule template for exactly what you want to do.
Why would they recommend using isapi (an old technology) over URL Rewrite, which is a new technology? There's a GUI you can use through IIS, although I appreciate you can't use that on shared hosting. However, all the GUI does is creates an XML file (web.config) in your web root with the rules in.
Use the GUI on your dev server and copy up the web.config with the rest of your files, job done.
thanks for the advice Owain, I've already done that. here is the code :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="domain.com" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
<rule name="redirect index.cfm to www.domain.com" patternSyntax="Wildcard" stopProcessing="true">
<match url="index.cfm" />
<conditions>
</conditions>
<action type="Redirect" url="http://www.domain.com/" />
</rule>
<rule name="rediret IP address to www.domain.com" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="11.222.333.444" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
is everything ok with the code above? is there anything I should change to
1. redirect non-www version to www
2. redirect IP address to http://www.domain.com/
3. redirect index.cfm to http://www.domain.com/
I spent several hours reading/learning more about IIS URL Rewrite Module and I can safely say that I'm simply amazed at how well it works! So, who on earth would use ISAPI_Rewrite ever again as both simple and more sophisticated rules can be added easily with URL Rewrite 2.0 (for advanced rules you need to be familiar with regular expressions ofcourse). Anyway, I will continue to read about this new technology and hopefully will be able to help someone if needed in the future ![]()
"Does it do what you expect it to?" , yup, it works like a charm
North America
Europe, Middle East and Africa
Asia Pacific