Remove www from your url in ASP

This assumes your default file is default.asp. If it is not default.asp, then you will need to change it. This is important to prevent endless looping.

Change example.com on line 7 to your domain name. This will do a permanent redirect to the non-www version of your domain. Place this at the top of your file.

So why would I want to do this?

  1. If you prefer to use the non-www version of your site. (http://example.com)
  2. Consistent URL referencing is a basic key to good SEO (search engine optimization).

Here is the code below, you will want to place this at the top of your page.

<%
PATH_INFO = lcase(Request.servervariables(“PATH_INFO”))
if PATH_INFO = “/default.asp” then
PATH_INFO = “”
end if
HTTP_HOST = lcase(Request.servervariables(“HTTP_HOST”))
if instr(HTTP_HOST, “www.”) = 1 then
newurl = “http://example.com” & PATH_INFO
Response.Status = “301 Moved Permanently”
Response.AddHeader “Location”, newurl
Response.End
end if
%>

This entry was posted in asp scripts and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>