URL Rewrites and IIS / ASP

posted on 15 December 2007 | posted in Coding


It's important to use search engine friendly URLs for your website. This aids search engines in understanding the content of your web pages.

Friendly URLs and Unfriendly URLs

An unfriendly URL:-

http://www.yourwebsite.com/products.asp?PageID=3422&CID=2&SKU=231444

A friendly URL:-

http://www.yourwebsite.com/Running-Shoes/Nike-Trainers/231444.html

A search engine gets no clue from the unfriendly URL as to the nature of the web page. It's just a string of codes. The friendly URL is full of helpful keywords for search engines. Not only that, but if someone links to this page via a forum or blog (for example), those keywords double up as very useful anchor text (as most people link using the URL itself as the anchor text).

It's not only search engines that find this kind of URL helpful - people do to! If someone is linking to a page in a forum or a blog, it's better that the link includes useful keywords as this helps people decide what the page is about before they decide to click on it.

How to create friendly URLs in ASP / IIS

Let's convert the following URL:-

http://www.yourwebsite.com/products.asp?ID=213

....into.....

http://www.yourwebsite.com/Nike-Running-Shoes/213.htm

If you were to try and access the converted friendly URL now, you would receive a 404 error in your browser, since the page doesn't exist! Why do I tell you this obvious fact? Because the first step is to create a "go to" file when a 404 error occurs on your website.

1. Create a blank ASP page and call it "404.asp" - save it. In this ASP page, copy the following code:-

<%
strQ = Request.ServerVariables("QUERY_STRING")
' Find the Product ID Number
nIndex = InStrRev(strQ,"/")
If (nIndex>0) Then
strProdID = Right(strQ,Len(strQ)-nIndex)
strQ = Left(strQ,nIndex-1)
End If

' Trim Off .htm From ProdID
If InStr (strProdID,".htm") THEN
strProdID =Left(strProdID,Len(strProdID)-4)
End If
intProdID=Cint (strProdID)
Server.Transfer("products.asp?ID=" & intProdID)
%>


....you will need to amend the above code to suit your particular application/code. Save the 404.asp file once more and upload it to the root folder of the website.

2. Now you will need to make some amendments in IIS.

Go into IIS on the server, and right-click on the website in question (under Web Sites).

Select Properties, then the Custom Errors tab. Now select 404 in the HTTP errors list. Click on Edit and make sure Message Type is set to "URL". Finally enter the URL : /404.asp (including forward slash at beginning). Finally Apply these settings.

Conclusion

This code in part (1) should be customised to suit your ASP code and how your web application works. As long as the Server.Transfer line passes the correct parameter it will work.
 

recommend a friendRecommend this blog entry to a friend