Tuesday, November 07, 2006

Resolving Canonical Domain Issue on Shared Host

I noticed recently that www.kesphelps.co.uk was accessible via the www and non-www version of the URL (or URI). I contacted my hosts to ask if IIS could be configured to permanently redirect the non-www version to the www version.

The issue this produces is known as the Canonical Domain Issue and is discussed here on Matt Cutts Blog. The problem is that the search engines can index both versions of the URL and may hand out a duplicate content penalty.

The host advised that their server was not configured this way by default. This is quite common on shared hosting packages and If you don't have htaccess or access to IIS then here is the solution:

Include this in all of your pages by either #INCLUDE'ing or placing the script in your config file.

This code will redirect any asp page to the www version'This code is provided as-is with no guarantee or 'warranty It is provided for free distribution as 'long as these comments are left in place.'Designed by www.jlh-design.com 2006Dim Domain_Name, theURL, QUERY_STRING, HTTP_PATH,TEMP_NUM'Get domain that the page is onDomain_Name = lcase(request.ServerVariables("HTTP_HOST"))'Check if URL is the www versionif left(Domain_Name, 3) <> "www" ThenHTTP_PATH = request.ServerVariables("PATH_INFO")'Check if page is default.asp if so, redirect to "/".'If other index page is used, such'as index.asp the numbers in the right and len statement ' need to be changed, as well'as the IF statment to indicate the index page.If right(HTTP_PATH, 12) = "/default.asp" ThenTEMP_NUM = len(HTTP_PATH)-11HTTP_PATH = left(HTTP_PATH,TEMP_NUM)End If' Sets the new URL settings with correct pageQUERY_STRING = request.ServerVariables("QUERY_STRING")theURL = "http://www." & Domain_Name & HTTP_PATH'This section passes on the query string variablesif len(QUERY_STRING) > 0 ThentheURL = theURL & "?" & QUERY_STRINGend if' Send 301 response and new locationResponse.ClearResponse.Status = "301 Moved Permanently"Response.AddHeader "Location", theURLResponse.FlushResponse.Endend if

The script doesn't just redirect but checks for the domain name the user used and then redirects accordingly, maintaining folder, file names and querystrings.

I #INCLUDE'ed the script across all pages on my server and the issue is now resolved. I shall report back the results after Google has re-indexed my site.

Credit: Thanks to softplus over at the cre8asite forums for putting me on to this one.

0 Comments:

Post a Comment

<< Home