Tag: Classic ASP

Poor man’s URL rewrite for IIS

Mod_rewrite is a very handy module that is included with most Apache installations and allows you to achieve some rather nifty stuff with a few lines in the .htaccess file.

IIS 6 doesn’t have a direct mechanism to compete with this but you can still achieve similar results using IIS’s error handling mechanism.

Start off by adding an error handling URL, in IIS Manager open the web site’s properties and select the Custom Errors tab:

IIS Custom Errors

Scroll down and find the 404 HTTP error entry, and then click on the Edit button.

IIS Custom Error Edit

Select URL from the message type drop down and enter your URL in the File: box, say “/404handler.asp”. NB the leading forward slash is required.

Click OK to save this and click OK in the properties dialog to apply the change to the website.

Now you need to create your 404 handler.

The first thing to note is that that in Classic ASP, Response.QueryString will hold a value which looks like the following:

404;/url-that-doesnt-match-a-file?x=2

Which means you wont be able to use Response.QueryString(“x”) to read the value 2, so you may want to refrain from using query strings, which is normally one of the reasons you are looking at implementing a URL rewrite scheme.

So at the start of the handler the first thing you will need to do is look at Response.QueryString and check that the first four characters are “404;”

If that’s the case then use the remainder of the string as the URL to be rewritten.

How you do this is really up to you, a simple scheme I’ve used is a look up table in the database to find the name of a script and then use System.Transfer to switch control to it. Remember that no variables declared and assigned in the first script are available in the script called by System.Transfer, but the Request object is still valid so that isn’t really a problem.

You can also use the same table to issue redirect to your SEO friendly URL if someone accesses it directly.

Related material:

Tags: , ,

Mask default home page in IIS

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: , , ,

Trojan Archer