ASP: mod_rewrite or URL Rewrite with Classic ASP (Sorta)

Using a Custom 404 error script in IIS makes it possible to emulate a very basic URL rewrite.

Sample Environment

  • IIS 6
  • Website domain: "www.me.com"
  • URLRewrite directory: "code"

Steps

  1. Create a file named rewrite.asp in the code directory.
  2. Add a Custom Error for the 404 Not Found. Open up IIS -> Right click Website -> Properties -> Custom Errors -> 404,URL,"/code/rewrite.asp" or you can do it programatically:
 1<%
 2on error resume next
 3myDomain = "www.me.com"
 4myrewritedir = "code"
 5
 6Set objW3SVC = GetObject("IIS://localhost/W3SVC")
 7For Each objSITE in objW3SVC
 8    If objSITE.class = "IIsWebServer" Then
 9        websiteNameArr = objSITE.ServerBindings
10        for j = 0 to Ubound(websiteNameArr)
11            websiteName = websiteNameArr(j)
12            
13            If instr(websiteName,myDomain) > 0 then
14                Set objIISNewDir = GetObject("IIS://localhost/W3SVC/" & objSite.Name & "/root")
15                Set CodeDir = objIISNewDir.Create("IIsWebDirectory",myrewritedir)
16                CodeDir.SetInfo
17                Set objIISNewDir = Nothing
18                
19                Set objIISRewriteRootDir = GetObject("IIS://localhost/W3SVC/" & objSite.Name & "/root/" & myrewritedir)
20                CustomErrors = objIISRewriteRootDir.HttpErrors
21                For i = 0 To UBound(CustomErrors)
22                    If Left(CustomErrors(i),3) = "404" then
23                        CustomErrors(i) = "404,*,URL,/" & myrewritedir & "/rewrite.asp"
24                        objIISRewriteRootDir.HttpErrors = CustomErrors
25                        objIISRewriteRootDir.SetInfo
26                        Exit For
27                    End If
28                Next
29                Set objIISRewriteRootDir = Nothing
30            End if
31        Next
32    End if
33Next
34%>
  1. Copy and paste the following code into rewrite.asp
 1<%
 2'In IIS, 404 pages that are directed to an URL have the "error" URL attached in the query string.
 3'It looks something like this 404;https://www.me.com:80/code/nosuchfileblahblah1.asp
 4'We're gonna use it.. so grab it.
 5
 6script = request.servervariables("QUERY_STRING")
 7if instr(script,"/") > 1 then
 8    myArray = split(script,"/")
 9    if instr(myArray(Ubound(myArray)),".asp") = 0 then
10        myID = myArray(Ubound(myArray))
11        'This is the method for obtaining the ID if you end your URL in the ID.
12        'Example: https://www.me.com/code/this-is-good-code/1
13    else
14        myID = replace(myArray(Ubound(myArray)),".asp","")
15        'This is the method for obtaining the ID if you end your URL in a fake extension.
16        'Example: https://www.me.com/code/this-is-good-code/1.asp
17    end if
18end if
19
20'Now that you've extracted your code ID, just go about your business, you are done!
21'Here's some sample code
22
23if isNumeric(myID) then
24    'Make sure it's an ID and not some malicious code
25    response.write "The ID Extracted from the URL is: " & myID
26else
27    response.redirect "https://www.me.com"
28end if
29%>
  1. Call your tricked out webpage URI.

I installed a sample of this script @ netnerds.net. Click on the following URL to see the results: netnerds.net/code/example-in-the-house/1. You can also look around RealCajunRecipes.com. I use that code (heavily modified) in the recipes directory to make SEO friendly URLs. Woo!