If you’ve ever wondered how to redirect users from a non-www (e.g. domain.com) to www (e.g. www.domain.com) URL when you’re using ASP (Windows Active Server Pages) on your website then wonder no longer.
By combining a simple 301 redirect with a check in the URL for the string “www”, you can ensure that users to your website always view the www version. Reasons for this could include ensuring that session state is maintained or for marketing purposes.
Implementation
If InStr(LCase(Request.ServerVariables("SERVER_NAME")),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.domain.com"
End If
Simply include this code at the top of any pages you want to redirect (ideally add it to an include file that is used on every page).
The code above isn’t perfect as any URL with www in the name will never redirect, such as http://wwwshop.com, but some simple string manipulation will fix that.
3 Responses to non-www to www redirect in ASP
Kiara Realty September 21, 2010
If i put this on individual files, will it redirect to the corresponding file? like, will something.com/abc.asp forward to http://www.something.com/abc.asp?
Or is there another server variable to see what filename is requested?
jack August 12, 2011
sorry, the code gives me
Server Error500 – Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
please help, as I’m looking for solution since long. thanks.
Joff August 13, 2011
Hi Jack,
Often the error 500 can be shown because you’ve not got IIS configured to display detailed error messages. I don’t know your specific hosting/server configuration, but this page may help: Show detailed error on IIS6 and IIS7. It’s not specific to Godaddy, so just follow their examples.