Skip to main content

URL Rewriting in Classic ASP

What is URL rewriting?

URL rewriting is usually used to make a URL for a dynamic web page more presentable, either for the reader or for search engines.

For example, a regular Classic ASP URL might look like this:

http://www.ford.com/page.asp?model=explorer&year=1999
A more presentable way of rewriting the URL might be:

http://www.ford.com/explorer/1999/
It used to be that search engines would not index pages with querystrings (the information after the .asp? in the URL). These days this is not the case and most search engines do index these types of pages. However, it has been suggested that major search engines might not be able to use more than three querystring parameters.

Also many search engines do look for keywords in the URL, but probably not in the querystring.

If you can remove the querystring altogether then you may stand a better chance with the search engines.

How to do URL rewriting in Classic ASP?

Unfortunately Classic ASP can't usually recognize URLs like this unless you happen to create static pages in each location.

For dynamic database-driven pages the simplest solution is to use an ISAPI filter but these cannot not always be installed, depending on your web host.

There are a couple of methods that can be used to do this in Classic ASP without installing additional components.

URL redirection in ASP using custom error pages
If you are able to set up custom error pages then you can create an error page that can translate these rewritten URLs and transfer to the correct page.

For a relatively simple to implement example of this:

Use the following script in your custom error page:

strQuery = Request.ServerVariables("QUERY_STRING")
strPage = Mid(strQuery, InStrRev(strQuery, "/") + 1)

' Change page.asp and page2.asp and add duplicate lines for
' each page you want redirection
If strPage = "page.asp" Then
Response.Status="200 OK"
Server.Transfer("/page.asp")
ElseIf strPage = "page2.asp" Then
Response.Status="200 OK"
Server.Transfer("/page2.asp")
Else
'Put your normal error page here
End If
You need to modify it a little. Change all of the page.asp and page2.asp to your own page names and add more of these lines if necessary. And add a custom error message after the Else line so that normal errors are still display when needed.

Add the following function to the beginning of each of your scripts that will use URL redirection:

Function RequestQS(strParam)
strQuery = Request.ServerVariables("QUERY_STRING")
lngStart = InStr(strQuery,"/" & strParam & "/")
RequestQS = ""
If lngStart > 0 Then
lngStart = lngStart + Len("/" & strParam & "/")
lngEnd = InStr(lngStart + 0, strQuery, "/")
If lngEnd > 0 Then
RequestQS = Mid(strQuery, lngStart, lngEnd - lngStart)
End If
End If
End Function
Then in your scripts, instead of using the usual request/request.querystring/etc to retrieve your paramaters, use RequestQS(param-name).

When calling the page use the following format:
http://www.mydomain.com/param1name/param1value/param2name/param2value/page.asp
You can then request the values of param1name, param2name etc by using RequestQS. (NB. You can include as many parameters as you need.)

This example lacks any protection against characters that might cause problems in directory names. If the parameter had a / (forward slash) in it then it will cause problems. You are best to pass only text or numbers and perhaps a few other symbols like the minus sign. This may require additional functions to convert then reconvert unwanted characters.

A few negatives to using custom error pages have been suggested. One is that the error may be logged so if you are using this method your error logs may be huge. Another is that this may take up extra server processing time but I personally doubt there would be a significant difference.

URL redirection in ASP using subdomains

Another interesting possibility, for which I am not sure of the consequences to search engines, is using subdomains to store parameters. This eliminates the need to use custom error pages and puts keywords into the subdomain which may be treated as more important than directories by some search engines.

For example, using the sample example URL as above:

http://www.ford.com/page.asp?model=explorer&year=1999
A more presentable way of rewriting the URL using subdomains might be:

http://explorer.1999.ford.com/page.asp
This, of course, would depend on whether your hosting company allowed wildcard subdomains.

Here is an example of simple subdomain URL rewriting:

strServer = lcase(Request.ServerVariables("SERVER_NAME"))
' Change mydomain.com to your domain name and duplicate
' this line for additional domain names
strServer = Replace(strServer, "mydomain.com", "")
If strServer = "" Then strServer = "."
strParams = Split(strServer, ".")

Function RequestSD(intParam)
If intParam > UBound(strParams) Then
RequestSD = ""
Else
RequestSD = strParams(intParam)
End If
End Function
You need to add (or include) this to the beginning of each script that you want to use the subdomain(s) as parameters. Make sure you change mydomain.com to your own domain name (don't include any http:// or www.) and duplicate the line for any additional domain names that might also be used.

Then to retrieve parameters use the RequestSD(parameter_number) function. For example, use RequestSD(0) for the first parameter, RequestSD(1) for the second and so on.

This example also lacks any protection against characters that might cause problems. If the parameter had a . (full stop) or a / (forward slash) in it then it will cause problems. You are best to pass only text or numbers and perhaps a few other symbols like the minus sign.

Update: There are now other ways to do this that might be simpler, for example the IIS7 URL Rewrite Module. However, the custom error page option can possibly give you more control.

Comments

Popular posts from this blog

Test Post

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam turpis mi, mollis quis rutrum ut, dictum nec arcu. Ut eget vestibulum est, vitae luctus lectus. Cras vitae dui blandit, finibus lacus in, lacinia sapien. Sed dictum a nisl dictum iaculis. Proin viverra fermentum ultricies. Suspendisse et enim in magna dapibus feugiat quis tincidunt ante. Donec venenatis nunc magna, eget fringilla ante efficitur non. Fusce purus justo, sodales eget vehicula aliquet, porta ut urna. Sed et dapibus massa. Mauris ac augue sit amet nunc tincidunt pretium sed sit amet turpis. Duis quis mauris sed libero faucibus vulputate at sit amet lacus. Phasellus leo turpis, interdum quis tristique id, congue a purus. Fusce lorem odio, pellentesque in condimentum in, semper id lorem. In efficitur lacus non lorem efficitur ultricies. Nulla viverra porta dolor, sed pulvinar neque porttitor in. Nam a libero eu mauris ornare rhoncus vitae ut felis. Nullam quis auctor nisi, at porta justo. Vestibulum ante ipsum primi...

Why your preventative medicine never works out the way you plan

9 things that won't happen in fitness programs. Why nutrition facts should be 1 of the 7 deadly sins. Why you'll never succeed at vaccination schedules. How fitness magazines are making the world a better place. Why our world would end if vaccine ingredients disappeared. 13 myths uncovered about healthy eating tips. 15 least favorite travel vaccines. How healthy eating meal plans are the new healthy eating meal plans. The 11 worst weight loss supplements in history. The 13 worst healthy eating tips in history.

Another test of notifications

 Another test of notifications