"Imagination will often carry us to worlds that never were. But without it we go nowhere " - Carl Sagan

blogs.she7ata.com, mohammed shehata personal showcase and blogs website.

she7ata.com » blogs » articles » URL rewritings on Classic ASP 3.0 without IIS configurations!

URL rewritings on Classic ASP 3.0 without IIS configurations!

published at:9/6/2009, Classic ASP,
published by: she7ata.com

Salam Alikum and Hello there,,,

I found this questions online, some people still using classic ASP like me and asking is there is anyway to do a real url-rewriting in classic ASP? and is that really possible without IIS configuration ?
 

All those new .net jeeks can easily implement a simple sitemap.xml to perform a simple url-rewriting without much effort, but in classic ASP the life is really hard, but more logical, at least for me… 

In this article I will show you how to create a really dynamic url-rewriting in classic ASP without configuring your IIS or anything . 

First what is URL-rewriting ?

I consider you know what it is, but I will tell you again, simply it is a different way to present a page on your site using a fake/not-real url

Why are we using URL-rewriting ?

Four reasons…

First, the url would look much elegant and expressive, and you can choose any or multiple names and extensions to one or more pages .
 

Second, because urls to pages and contents are much elegant and expressive, search engines can easily crawlers site pages, especially with .html extension and without much query strings and question marks.
 

Third reason, is to keep your site without any broken links, say you have an old links, you still can redirect those links to new pages without losing visitors by letting them see the nice Page Not Found message.
 

Reason number four, is 'Security', yes, no one knows what is your real Asp pages, it will be hard for normal users to know what is your server language or platform, and/or you can frequently change the url of a certain page to anything, more over you can redirect multiple links to one page, and of course you're sure any update to the original page will reflect on those links' pages.
 

How it looks like ?

A url like …
http://mywebsite.com/blogs/blogViewer.aspx?articleID=123
after applying url re-write it would be like
http://mywebsite.com/articles/how_to_create_url_rewriting.htm

Please notice, there is no such htm page on your web server, but as you see, it works and the url looks ok, and visitors can even memorize it, but for a normal user to remember a page like which have a lot of query strings and much number or IDs.

Okay Okay, so how to do it on classic ASP.

Actually there are two ways to do url-remapping or rewriting …


Method number one is very simple and very useful if you want to map urls to static pages, and this is would work also for sub-domains, i.e ahmed.she7ata.com, is actually mapped to sub-folder called ahmed underneath the domain name she7ata.com, also for sitemap.xml file, you can generate dynamic sitemap.xml by mapping this xml file to an ASP page which generate this file.


Method number two is a bit complicated and it is very useful if you want to dynamically map pages, i.e blog article pages or categories like this on my she7ata'sBlogs.
 

Finally, the two methods are based on your custom error page, yes, this is the key, you will need to customize your 404 error page and change it to error.asp for example.
Most of web hosts will give you this option through your CP a.k.a Control Panel, if you do not have this option use the default 404 error page and redirect it to your asp-page any way.

Now to the implementation .
 

A very nice thing that the IIS sends the error link as a querystring and/or on the http header to your 404 error page, and this is can be retrived either by fetching the server variable QUERY_STRING or directly through the REQUEST Object.


step 1
On your custom error page error.asp write the following code.

response.write("Querystring is " & Request.ServerVariables("QUERY_STRING") )

try to write any link to any page that not exist in your website… say http://www.she7ata.com:80/blogs/index.html

a white blanked page will appear with one line of text like following :
Quertstring is 404;http://www.she7ata.com:80/blogs/index.html

step 2
in that final step you need to do some if conditions to map fake urls to real asp pages. Try the following code


if instr(Request.ServerVariables("QUERY_STRING"),"www.she7ata.com/blogs/index.html")>0 then
        %><!-- #Include File="myBlogsIndex.asp" --><%
end if

or
select case lcase(Request.ServerVariables("QUERY_STRING"))
case "http//:xxx/file1.htm"
      %><!-- #Include File=" file1.asp" --><%
case " http//:xxx/file2.htm "
      %><!-- #Include File=" file2.asp" --><%
case "404;http://xxx/sitemap.xml
      %><!-- #Include File="sitemap.asp" --><%
case else
     'generate a real error page or save this to your log file
      response.write "page not found "
end select.


Please note, we used the asp 'file include' routine so that asp will include the asp file within this error page itself, resulting the right content, nothing will change the url on the browser, please also note, if you used response.redirect, it will work, but of course the url on the browser will change to your target asp page, do not do that unless you want it.

On method number two, dynamic url with query strings

On this method we will need to split parts of the url and keep them into a session object to exchange them with the targeted asp page which will generate the content based on those urls and IDs

Like the above code we can do the following

if instr(Request.ServerVariables("QUERY_STRING"),"www.she7ata.com/blogs/articles/11/index.htm")>0 then
          session("articleID")= mid(Request.ServerVariables("QUERY_STRING"),49,2)
          %><!-- #Include File="blogArticle.asp" --><%
          ' you can use also server.transfer
end if


on the blogArticle.asp
use the session object we create to get the ID to query the article from your database or whatever.

Dim myArticleID
myArticleID= session("articleID")


That's all folks
Have a nice rewriting
Shehata.


  categories

 » recent posts

 � recent replies

 » friends & links


6 COMMENT(S)

comment

Wesley | 3/5/2011

Hi, I did through your instruction but it didn't work. I probably did something wrong. Could you help me please? Here is my link: https://www.bellafindings.com/jewelscart2000/store/jewelscart2000_listCategoriesAndProducts.asp?idCategory=6 I want to be a static link. thanks
comment reply, she7ata

mohammed | 3/7/2011

Hi, I checked the link, its seems jewelscart2000 is a ready web app based on ASP, you will have to do some coding to make it work, first to customize your 404 error page, that would be from your host or the cp, redirect the 404 error page to another asp page, then you will need to add some code on the asp page like the above to read the parameters from the url and redirect/transfer to the desire page, that means you will have to do some extra code or the jewelscart2000 source files as well. Can you ask the jewelscart2000 developers to include the url-rewriting mode on the jewelscart app, this is can be easier solution for you. I hope that helps :-).
comment

Papirri | 7/17/2010

Excelent code but if you have some time I would like to answer one question I have (I used one of your codes and works fine with one case and one include file) once I use more than one nothing happends please reply
comment reply, she7ata

mohammed | 7/17/2010

Thanks for your question and comment, in fact i need to know which method you used? custome 404 error page or what? beside that it could be an issue with redirecting code, please email me this part of the source code to take a look.
comment

urko | 1/30/2010

im new to asp so im not really sure how to do this correctly. I user candypress .asp store and would like to add url rewrite. what exactly must i write in my custom error page and what exactly must i write on my product list page so that it will redirect to cusotm error page and show friendly url
comment reply, she7ata

mohammed | 1/31/2010

Hello Urko, you need a minimum knowledge of ASP to do so, you need to be able to customize your 404 error page from the control panel, then you need to edit your 404.asp page and program it the way you like, simple if statement like the samples above can work also, but if you need to redirect a lot of dynamic product list, you need to do some advanced workaround, this is useful for optimizing your site for better SEO, you can also ask your CandyPress support team about this issue to help you out, good luck.

ADD COMMENT

name:*
email:*
website:
comment:
captcha:
she7ata.com, owned and managed by mohammed shehata
(C) 2000~2009
QUICK LINKS BLACKHAND, ELWARSHA, BLOGS, PORTFOLIOS
she7ata'sBlogs v1.0, beta, not just another wordpress or joomla site!