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:
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
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
%>
<%
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:
Post a Comment