Friday, September 08, 2006

WWW Versus Non-WWW URLs: Dupe Content and Redirecting

I'm not real clear on the technical reasons behind it all, but the consensus in the SEO community is that it's detrimental because of duplicate content penalities to have your web site operating on both the www and non-www addresses. Ideally, you should pick one address or the other and have visitors (human and otherwise) who come to the other forwarded using a permanent redirect. So if you choose www.YourSite.com as your web web address, you should have all visitors to Your.Site.com forwarded to www.YourSite.com.

This is easy enough to do for one page, but how about some handy code that will automatically forward all your pages using a search engine friendly 301 redirect?

This code that will forward any visitor to the www version of your site, no matter what the entry page. Place this code at the top of every page:

<%
Dim strDomain, strURL, strQueryString, strHTTPPath,vTempNum

'Get page domain
strDomain = LCase(request.ServerVariables("HTTP_HOST"))

'Check for www
If Left(strDomain, 3) <> "www" Then
strHTTPPath = Request.ServerVariables("PATH_INFO")

'If page is default.asp, send to root
If right(strHTTPPath, 12) = "/default.asp" Then
vTempNum = Len(strHTTPPath)-11
strHTTPPath = Left(strHTTPPath,vTempNum)
End If

'If page is index.asp, send to root
If right(strHTTPPath, 10) = "/index.asp" Then
vTempNum = Len(strHTTPPath)-9
strHTTPPath = Left(strHTTPPath,vTempNum)
End If

'Set the new URL
strQueryString = Request.ServerVariables("QUERY_STRING")
strURL = "http://www." & strDomain & strHTTPPath

'If any, pass on query string variables
If len(strQueryString) > 0 Then strURL = strURL & "?" & strQueryString

'301 Redirect to www version
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", strURL
End If
%>
Likewise, here is code that will redirect to the non-www version:

<%
Dim strDomain, strURL, strQueryString, strHTTPPath,vTempNum

'Get Page domain
strDomain = lcase(request.ServerVariables("HTTP_HOST"))

'Check for www
If Left(strDomain, 3) = "www" Then
'Change to non-www version
vTempNum = Len(strDomain)-4
strDomain = Right(strDomain,vTempNum)
strHTTPPath = Request.ServerVariables("PATH_INFO")

'If page is default.asp, send to root
If right(strHTTPPath, 12) = "/default.asp" Then
vTempNum = Len(strHTTPPath)-11
strHTTPPath = Left(strHTTPPath,vTempNum)
End If

'If page is index.asp, send to root
If right(strHTTPPath, 10) = "/index.asp" Then
vTempNum = Len(strHTTPPath)-9
strHTTPPath = Left(strHTTPPath,vTempNum)
End If

'Set new URL
strQueryString = Request.ServerVariables("QUERY_STRING")
strURL = "http://" & strDomain & strHTTPPath

'If any, pass on query string variables
if len(strQueryString) > 0 Then strURL = strURL & "?" & strQueryString

'301 redirect to non-www version
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", strURL
End If
%>

No comments: