Non-WWW to WWW Redirect on IIS 6.0 with httpd.ini

Most of the websites I work on are on Linux/Apache servers so I can easily add 301 redirects via the .htaccess file. However, my new client is on an IIS server for which I don’t have access to so I can only add redirects via the httpd.ini file and then send back to the client to upload for me. I figured out how to add 301 redirects in the httpd.ini file at the page level, but I am having the hardest time trying to find a good resource about how to redirect the non-www version of the website to the www version. The following line redirects at the page level:

RewriteRule /oldpage.asp http://www.site.com/newpage.asp [I,O,RP,L]

But that obviously won’t work for redirecting the non-www to the www version of the site. It’s not an issue of duplicate content, but rather the non-www version simply shows a “Under Construction” error message while the www version works just fine. So it’s even more critical that the non-www be redirected to the www version. However, searches on Google to answer my question have been fruitless thus far. Most of the results are web forums with threads from people asking the same question I am asking and then of course, the question is never answered or the original poster figures it out on their own and never posts the answer in their thread, both of which are worthless to me as somebody looking for that answer.

I came across one forum which provided the solution below, but I’m not 100% sure it will work as expected…

RewriteCond %HTTPS off
RewriteCond Host: (?!www\.)([^.]+\.[^.]+)
RewriteRule (.*) http\://www.$1$2 [I,RP]

And then another blog post had the following:

# Move anything from non- www.example.com -> www.example.com
# e.g. example.com -> www.example.com
RewriteCond %HTTPS off
RewriteCond Host: (?!^www.example.com)(.+)
RewriteRule /(.*) http\://www.example.com/$2 [I,RP]

Has anybody had success redirecting non-www versions of pages to www versions on IIS using one of the two methods above?

Comments

Leave a Reply