/home/web-programming/

ASP Coding for 301 Redirect to www.

Simple ASP 301 Redirect

To tell a search engine that the content of a particular page has moved to another page, use this coding:

<%
   Response.Status = "301 Moved Permanently"

   'Change the "page-name" below to fit your needs
   Response.AddHeader "Location", "/page-name.asp"
%>


301 Redirect to Solve Duplicate Content Issues

Some search engines consider your domain with the www. and without the www. as two separate web sites. This is an issue because those same search engines penalize sites for duplicate content.

Test for 301 Redirect

Some servers are setup to automatically redirect the website to the www. version of the domain and some are not. An easy test is to type your domain in the browser address bar including the www. and then without the www. and see what happens. Does one of them roll automatically to the other? If yes, there is nothing more for you to do. If no, add 301 Redirect coding to the top of every page, then try the test again.

Adding the 301 Redirect

First choose which way you want your website url to read. Either www.whatever.xxx or whatever.xxx, but not both. Microsoft.com, Adobe.com, BofA.com all roll to the url with the www., so I chose to do the same, but you can do it the other way around if you like.

ASP Coding for 301 Redirect to www.

Add this coding to the top of every page as an include file. This coding redirects to the www. version of the domain, but can be modified to provide the reverse results.

<%
Dim Domain, HTTP_HOST, PATH_INFO, QUERY_STRING, URL

'Enter your domain below
Domain = "www.DomainGoesHere"

HTTP_HOST = lCase(Request.ServerVariables("HTTP_HOST"))

If HTTP_HOST <> Domain Then

   PATH_INFO = Request.ServerVariables("PATH_INFO")

   'If your homepage is not "default" then change the name below
   'and change the number 8 to the appropriate number of characters.

   If Left(PATH_INFO,8) = "/default" Then PATH_INFO = ""

   QUERY_STRING = Request.ServerVariables("QUERY_STRING")

   URL = "http://" & Domain & PATH_INFO

   If Len(QUERY_STRING) > 0 Then URL = URL & "?" & QUERY_STRING

   Response.Status = "301 Moved Permanently"
   Response.AddHeader "Location", URL

End If
%>

Be sure to retest the pages with the www. and without to see that the coding is working properly.