301 Redirects in ASP on an IIS Server
Individual Page 301 redirects
To forward individual pages to a new location in IIS, please see this post.
www to non-www or non-www to www 301 redirects
To fix canonicalization issues of your domain in when using asp on an IIS server, specially in a shared hosting environment, sometimes you need to enter the code in on a page by page basis. Having your host set up the server to parse asp on all pages allows you to use this method on htm and html pages as well.
In developing the code for this I wanted it to have several functions:
- Be generic so I don’t need to enter the domain, so I can use the same file on all my domains.
- Pass on any server variables for dynamic pages.
- Redirect the default.asp page to “/”
- work in any subfolder as well for the default.asp fix.
At the top of each page on the site I place an included file calling up either version. The download(below) includes code to forward to the non-www and the www version of your site.
To forward to the WWW version via 301 redirect:
<%
'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 2006
Dim Domain_Name, theURL, QUERY_STRING, HTTP_PATH,TEMP_NUM
'Get domain that the page is on
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
'Check if URL is the www version
if left(Domain_Name, 3) <> "www" Then
HTTP_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" Then
TEMP_NUM = len(HTTP_PATH)-11
HTTP_PATH = left(HTTP_PATH,TEMP_NUM)
End If
' Sets the new URL settings with correct page
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://www." & Domain_Name & HTTP_PATH
'This section passes on the query string variables
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
' Send 301 response and new location
Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if
%>
To forward to the non-www version via a 301 redirect:
<%
'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 2006
Dim Domain_Name, theURL, QUERY_STRING, HTTP_PATH,TEMP_NUM
' Get domain name the page is on
Domain_Name = lcase(request.ServerVariables("HTTP_HOST"))
' Check to see if www version
if left(Domain_Name, 3) = "www" Then
' Changes http path to non-www version
TEMP_NUM = len(Domain_Name)-4
Domain_Name = right(Domain_Name,TEMP_NUM)
HTTP_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" Then
TEMP_NUM = len(HTTP_PATH)-11
HTTP_PATH = left(HTTP_PATH,TEMP_NUM)
End If
' Sets the new URL settings with correct page
QUERY_STRING = request.ServerVariables("QUERY_STRING")
theURL = "http://" & Domain_Name & HTTP_PATH
'This section passes on the query string variables
if len(QUERY_STRING) > 0 Then
theURL = theURL & "?" & QUERY_STRING
end if
' Send 301 response and new location
Response.Clear
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", theURL
Response.Flush
Response.End
end if
%>
Download
If you liked this post please buy me a beer. Thanks.posted in Webmastering | 10 Comments



