David Behan's Web Design Ireland Blog

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.

You can see a working example of this by trying to visit http://davidbehan.com/?test=ok. It should redirect to http://www.davidbehan.com/?test=ok.

‘Collect url information being requested
svrHttps = request.servervariables("HTTPS")
svrHost = request.servervariables("HTTP_HOST")
svrUrl = request.servervariables("URL")
svrQueryString = request.servervariables("QUERY_STRING")

 
‘Check if www appears in the domain and begin building url to redirect to if it doesn’t

IF left(svrHost,4) <> "www." THEN

 
‘Check if the page is secure or not

IF svrHttps = "off" THEN

svrNewUrl = "http://www."

ELSE

svrNewUrl = "https://www."

END IF

 
‘Add the domain, folder(s) and page requested as well as remove directory indexes

svrNewUrl = svrNewUrl & svrHost & svrUrl
svrNewUrl = replace(svrNewUrl,"default.asp","")
svrNewUrl = replace(svrNewUrl,"index.asp","")

 
‘If there is a querystring, add it to the redirect link

IF len(svrQueryString) > 0 THEN

svrNewUrl = svrNewUrl & "?" & svrQueryString

END IF

 
‘Do the actual 301 redirect to the newly constructed url

response.status = "301 Moved Permanently"
response.addheader "Location", svrNewUrl
response.end

 
END IF

17 Comments (post a comment)

gravatar

stanb  spacer

You can use IIS Mod-Rewrite ( http://www.micronovae.com/ModRewrite/ModRewrite.html ) and make redirects on IIS the same exact way you do with apache mod_rewrite. It also supports htaccess (I think it’s only on “pro” version).

There are also other tools such as IIRF ( http://cheeso.members.winisp.net/IIRF.aspx ) and IIS Rewrite (http://www.qwerksoft.com/products/iisrewrite/) that work in a similar way, but are not fully compatible with mod_rewrite and do not support htaccess.

May 29, 2007 @ 9:10 am
gravatar

David  spacer

Hi Stan.
You are right about IIS Mod-Rewrite and it is a great application. The pro version allows you to have htaccess files on a per domain basis while the free version only allows it on a server wide basis. The solution above is for people who do not have access to install applications on their server or have access to IIS, i.e. shared hosting. If you have a dedicated server or can get IIS Mod-Rewrite installed… I’d recommend that route.
Rgds, Dave

May 29, 2007 @ 9:20 am
gravatar

stanb  spacer

Hi David,

You’re right, you will rarely find IIS shared hosting plans that support such tools.

Also, coding your own redirection script (such as your ASP script above), sometimes is preferable, especially when the redirection logic gets too complicated and depends on factors or resources not related to HTTP protocol, such as database stats, user activity etc.

May 29, 2007 @ 11:30 am
gravatar

David  spacer

Indeed, hopefully this will be of some benefit to some people. I have a load of scripts that I’ve written for different projects. I think it might be time to start publishing them. :D

May 29, 2007 @ 11:38 am
gravatar

rotokirby  spacer

Thanks for the script….works as advertised. I just inserted it into the header file for our site and it does the trick fast and clean!

Do you know if there is any advantage in using IIS Mod-Rewrite (performance or SEO) over a script such as this? I could purchase and install Mod-Rewrite but this is working already. :)

August 2, 2007 @ 4:43 pm
gravatar

David  spacer

Hi rotokirby,

Your very welcome! If you can get IIS mod rewrite, I’d recommend it. This solution is really for shared hosting where you can’t install mod rewrite. With this method you can’t really have subfolders like davidbehan.com/services/webdesign/ireland or something like that where it is pretty easy to do with IIS mod rewrite.

Rgds, Dave

August 2, 2007 @ 4:57 pm
gravatar

FaraFiro  spacer

This works great. How can I remove the “default.asp” so it loads without it like your website?

August 8, 2007 @ 11:26 pm
gravatar

David  spacer

Hi FaraFiro,

Thanks for your comment. Actually, you should just change your links to point to the domain/folder without the default.asp attached. If the default directory indexes are set up, it should pick up the default.asp file automatically.

In the code above, there is a line [ svrNewUrl = replace(svrNewUrl,"default.asp","") ] that removes the default.asp from the link as well so it should work for you fine.

Rgds, Dave

August 9, 2007 @ 6:26 am
gravatar

Steve Mac  spacer

hello
thanks for code, good work david for the solution.I tried it yesterday it was working amazing but i wanted to know is work on firefox.I am sure this example going to help me in my future work.Great and thanks for all your comments.

September 24, 2007 @ 9:32 am
gravatar

David  spacer

Hi Steve. This solution should be browser independent as this code runs on the server and not in the browser.

September 25, 2007 @ 6:31 am
gravatar

David  spacer

As an update to this post… I recently installed ISAPI ReWrite (www.isapirewrite.com) onto one of my servers. I wanted to get the whole folder thing working for a client site and I’d highly recommend version 3 of the software. It’s got great support for migrating htaccess files from linux. Actually, I’ll do a full review of it shortly.

September 25, 2007 @ 6:34 am

[...] little article and script for when you don’t have isapi rewrite that you might find interesting: blog ยป ASP: NON-WWW 301 Redirect on IIS | davidbehan | web design – web development – web marketing … Rgds, Dave __________________ David Behan – Web Design Ireland [...]

December 4, 2007 @ 4:35 pm
gravatar

Yairba  spacer

Is it possible to put a file on IIS, something like .htaccess?
A customer of mine has thousands of pages, and I don’t want to add the script to every page.

May 14, 2008 @ 1:36 pm
gravatar

David  spacer

You can try http://www.isapirewrite.com. I have version 3 running on IIS and it works great. You just use htaccess as you would on a linux.

May 14, 2008 @ 4:56 pm
gravatar

Rick Lim  spacer

Thanks for this tutorial. I had been wondering how to do that until i read this.

August 23, 2008 @ 1:59 pm
gravatar

triple5iks  spacer

omg…

thank you very much ^:)^

working great ;)

January 25, 2009 @ 2:31 am

Thanks for this tutorial Dave, it came in really useful for a client.

June 5, 2009 @ 5:35 am

RSS feed for comments on this post · TrackBack URI

Leave a Comment