Recently a SEO consultant noted that on a site I was working on the home page was listed on two urls:
http://www.example.com/
http://www.example.com/default.asp
He suggested that any attempt to visit /default.asp directly should 301 redirect to /.
The obvious approach seemed to be to use Request.ServerVariables(“url”) to see what had been requested, and here I found a problem in IIS 6 (but not in IIS 7). IIS 6 rewrites the URL “/” to “/default.asp” so there was no way to tell which the user had requested and any attempt to redirect based on the server variable would cause a 301 infinite loop.
My solution was to change the default document setting in IIS to a document that did not exist and then register a 403;14 error handler.
Now when the user requests “/” IIS 6 cannot find the default document and instead tries to list the contents of the root directory. Make sure that Directory Browsing is disabled. This will cause a 403;14 error and our default.asp page will be called.
At the top of the default.asp page I add the following code:
dim arrqs
'
' If we are being called as an error handler then querystring will look like:
'
' 403;http://www.example.com:80/
'
if instr(request.querystring, ";") > 0 then
'
' If there is a semi-colon, split the string and check that the first
' substring is 403. If not you need to think about what you would like
' to do, out of laziness here I just put a 301 redirect to "/".
'
arrqs = split(request.querystring,";")
if arrqs(0) <> "403" then
response.status="301 Moved Permanently"
response.addheader "Location","/"
response.end
end if
end if
'
' Next if query string is empty I'm redirecting to "/" because that
' means someone is trying something other than "/".
'
if request.querystring = "" then
response.status="301 Moved Permanently"
response.addheader "Location","/"
response.end
end if
Fairly simply though not elegant.
IIS 7 doesn’t rewrite the URL server variable so it’s simpler. No need to have the default document set to a non existant page, and the start of default.asp is:
if Request.ServerVariables("URL") = "/default.asp" then
response.status="301 Moved Permanently"
response.addheader "Location","/"
response.end
end if
Related posts:
Tags: Classic ASP, IIS 6, IIS 7, URL rewrite
