ASP: NON-WWW 301 Redirect on IIS
I was doing some research on how to do a non-www 301 redirect on a Windows IIS server and I found the usual response.status and response.addheader solution, which also required access to IIS for spliting up the www and non-www part of your domain. If you are on a shared server, this isn’t always possible. I wanted a pure code version of the redirect that I could drop into my pages so that if somebody visited a page on the site that had sub folders and/or querystrings without the www part, it would preserve these and redirect to the www version. I couldn’t find exactly what I was looking for so I decided to write my own in ASP.
The script below should be inserted into the top of all the pages you want this to work. The best way to do this is using an include file or adding it to the top of one of your existing include files that runs before anything is written to the page. The pages also need to be ASP. This will not work on plain html pages. This isn’t a perfect solution as it will only work on actual pages but it will help a lot of people out there I think. On linux, this is much easier using the htaccess solution but as IIS doesn’t have this, this is a good alternative. If www exists in the URL, this script will not execute. You may want to do the same in reverse, as in redirect www to non-www URL’s. If there is a demand for this, I’ll modify the script to support it.