David Behan's Web Design Ireland Blog

Notice: 09/09/09

This blog is no longer updated with new content but has been left live as there's some relevant content that still attracts a lot of traffic. You can still post comments and I will reply when I can.

If you want to visit my new site and blog, you can do so at www.webtogether.ie and you can still contact me by email hi@davidbehan.com. I'm also on twitter: @davidbehan. Thanks for visiting!

Rgds, Dave

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

25 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
gravatar

Brendan  spacer

I work as a programmer for an SEO firm. This is a great script to have when most clients are on Shared Hosts, and do not have access to the IIS settings. Thanks Dave! This script worked beautifully with no configuration needed. Just note, when I copied and pasted, the comment ticks (’) became, a different symbol (`) so it raised an internal server error, quickly fixed.

August 8, 2009 @ 3:53 pm
gravatar

David  spacer

Yeah, I’ll have to have a look why that’s happening. CMS probably converted those automatically on save. Glad you like it.

August 14, 2009 @ 8:47 am
gravatar

Tony  spacer

Hi David,

Thanks for the script! It works perfect on all pages except the home page. The problem is; the script is adding default.asp to the domain.

Example: http://domain.com turns into http://www.domain.com/default.asp

How do I remove Default.asp?

Thanks!

August 18, 2009 @ 3:09 pm
gravatar

David  spacer

Hi Tony,

It shouldn’t add default.asp as there is a line in the code that actually removes that from the new url, i.e.

svrNewUrl = replace(svrNewUrl,”default.asp”,”")

The only thing I can think of is that it’s adding Default.asp maybe and isn’t removing it properly then.

If so, change this line:

svrNewUrl = svrNewUrl & svrHost & svrUrl

to:

svrNewUrl = lcase(svrNewUrl & svrHost & svrUrl)

and give it another go. If you want to pop me a url to see a live example, I might be able to diagnose.

HTH, Dave

August 23, 2009 @ 6:17 pm
gravatar

John  spacer

I’m having to deal with this issue for two different classic ASP sites at the same time – one on a dedicated server and the other on shared hosting.

One ASP/ASP.Net shared hosting company that offers the IIS Mod-Rewrite pre-installed on its servers is CrystalTech.

Your suggestion for dealing with non-www redirect for ASP is about the best one I can find. Thanks.

October 14, 2009 @ 2:47 pm
gravatar

Free  spacer

Hi David ;

thank you so much for your great code;

with ” svrNewUrl = replace(svrNewUrl,”default.asp”,””) ”

i research it on the net since a few months…
thank you thank you, again.. it s worked good

November 3, 2009 @ 12:58 am
gravatar

marky  spacer

hi! David,
I’ve tested your script and works fine… However my requirement is from http://www.domain.com to http://domain.com Any help of what should change along the line to make it work as I intended.
Thank for the help.

December 1, 2009 @ 2:16 am
gravatar

Nik  spacer

you can use IIRF filter.

RewriteCond %{HTTP_HOST} ^domain\.com$
RedirectRule ^/(.*)$ http:/www.domain.com/$1 [R=301]

January 24, 2010 @ 9:13 pm

RSS feed for comments on this post · TrackBack URI

Leave a Comment