URL Rewriting (Search Engine Friendly URLs) in ASP

18 05 2009

Search Engine Optimization (SEO) is very important for getting free quality traffic to your website. URL rewriting is an important aspect in SEO. Wondering what is URL rewriting? If you are familar with web development, you might be using querystrings.

For example, take an URL http://www.examplesite.com/page.asp?article=1234. For a search engine article=1234 does not mean anything. Instead if your webpage address is something like http://www.examplesite.com/page_1234_SEO-Optimization, it can make out that the page is related to SEO optimization and it can index your website properly.

The following article shows how to rewrite URLs in Classical ASP.

When we try to navigate to http://www.examplesite.com/page_1234_SEO-Optimization, as this is ficticious path, the IIS will throw 404 NOT FOUND error. We should capture this error silently and then forward the user to correct page.

1. Open the IIS Console and see the website properties. If you are using Vista, look for Error Pages feature for the website. (Windows XP users should find it in website properties dialog). If you are using Godaddy for hosting, you will find the option in website control panel.
2. For the 404 status code, set Execute a URL on this site to forward.asp.
3. Create a new webpage forward.asp with the following code.

<%

url = Request.Querystring
url = Right(url, len(url) – 4)
url= replace(url, “
http://www.examplesite.com:80/“, “”)
url = replace(url, “
http://www.examplesite.com/“, “”)
url = replace(url, “
http://examplesite.com:80/“, “”)
url = replace(url, “
http://examplesite.com/“, “”)

curUrl = split(url, “_”)

if curUrl(0)=”page” and curUrl(1)<>”” then
                  Response.Status = “200 OK”
                  server.transfer(“page.asp”)
else
                  response.redirect(“default.asp”)
end if

%>

4. Open Page.asp file. Your earlier logic might be to find which article should be displayed. Instead of this, we should parse the rewritten URL to find the article id. Insert the following lines of code in Page.asp.

<%

url = Request.Querystring
url = Right(url, len(url) – 4)
url= replace(url, “
http://www.examplesite.com:80/“, “”)
url = replace(url, “
http://www.examplesite.com/“, “”)
url = replace(url, “
http://examplesite.com:80/“, “”)
url = replace(url, “
http://examplesite.com/“, “”)

curUrl = split(url, “_”)


if(uBound(curUrl)>1) then
                  if curUrl(0)=”page” and curUrl(1)<>”” then
                         articleid=curUrl(1)
                  end if
else
                  response.redirect(“default.asp”)
end if

%>

We just changed the logic in Page.asp to find which article should be loaded. Earlier we used to find articleid from request.querystring(“article”). Now, we are reading the complete URL and then parsing the string to find the articleid. You might have to change the links across your site to link to the new URLs.

Advertisements

Actions

Information

One response

8 06 2009
TripAdvicer

thanks for your scripts. actually i got this to work at my site http://www.cambodia-tourism.org/news

nite .. tahnks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: