Click here to Skip to main content
15,885,278 members
Articles / Web Development / IIS
Article

URL cloaking with ASP.NET (C#)

Rate me:
Please Sign up or sign in to vote.
2.82/5 (9 votes)
10 Apr 20052 min read 79.1K   44   12
A short article on how to replace your long and ugly querystring URLs with clean and simple URLs in ASP.NET.

Introduction

Nowadays content management systems (CMS) are a commodity. These systems enable regular users to create and maintain websites with little or no technical skills at all. Usually CMS are built with databases and the pages in sites created in the CMS are accessed through a URL looking like this:

http://www.somecompany.com/Templates/page1213___.aspx

or

http://www.somecompany.com/content.aspx?pageID=1213 etc.

For an experienced user, this really doesn't matter, but from a usability perspective - it's illegal! If you're a regular user looking for information, URLs like the ones below probably would make more sense to you:

http://www.somecompany.com/product

or

http://www.somecompany.com/support

This is where URL cloaking comes in handy.

Background

To try this trick out, create an empty solution in VS.NET, add a C# ASP.NET Web application (Call it "Cloaking"). Create and add 404.aspx, content1.aspx, content2.aspx and content3.aspx to your solution. Make sure to fill them up with some content so that you can distinguish them from each other.

Your solution explorer should look something like this:

Solution Explorer

Setting a custom 404 errorpage

In order to pull this trick off, you need to configure IIS to use the customized 404 error page (in this example, 404.aspx). To do that, go to the IIS administration console, select the correct website (in this example the website is called "Cloaking"). Right click it and choose "Properties" then select the "Custom Errors" tab.

Website Properties

Find the 404 entry in the list of errors and click "Edit properties". Select URL in the "Message Type" dropdown and type the full path to the 404.aspx page in the URL textbox, in this example "/Cloaking/404.aspx".

It should look something like this:

Error Mapping Properties

Switch back to VS.NET, open the Global.asax file and view the code. Scroll down to the Application_BeginRequest method and paste the following:

C#
// The requested URL
string strURL = Request.RawUrl.ToLower(); 
    
// If the page didnt exist, the IIS tries to send the visitor to 404.aspx, 
//this is where we intercept.
if(strURL.IndexOf("404.aspx") > 0) 
{ 
    // Scan the path for the keywords and rewrite the path to the preferred content
    if (strURL.IndexOf("products") > 0) Context.RewritePath("content1.aspx");
    if (strURL.IndexOf("sales") > 0) Context.RewritePath("content2.aspx");
    if (strURL.IndexOf("support") > 0) Context.RewritePath("content3.aspx");
}

..and there you have it!

If you browse http://localhost/Cloaking/Products, the browser will display the contents of content1.aspx and maintain the nice URL in the address field.

The code uses the strURL.IndexOf() method to detect keywords, this is kind of crude and is just meant to illustrate the technique.

Good use of URL cloaking

In order to make good use of this trick, you'd want to implement it in a CMS system. To illustrate the possible usage, in your CMS you probably have a table containing the pages of the website. Extend the pages table with a column called "path" and search that column in the application_beginrequest method to find the appropriate page to redirect to.

Does it sound interesting? Drop a comment and maybe I'll post a simple CMS that implements URL cloaking.

History

  • 2005-04-10: Created

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
Emmanuel holds a BSc (Informatics). He is currently employed by a swedish consultant agency. His current interests include software architecture, enterprise application integration, commercial e-services and writing about himself in third person.

Comments and Discussions

 
QuestionIs there any way to catch 404 before IIS? Pin
xibeifeijian19-Mar-07 17:56
xibeifeijian19-Mar-07 17:56 
AnswerRe: Is there any way to catch 404 before IIS? Pin
Agenor20-Jun-10 23:20
Agenor20-Jun-10 23:20 
GeneralCool! This is exactly what I am looking for! Pin
Petrander5-Sep-06 21:26
Petrander5-Sep-06 21:26 
GeneralURL rewriting problems? need help. Pin
Preky15-Sep-05 22:52
Preky15-Sep-05 22:52 
in search for resolution i came to url rewriting.
Among other problems like postbacks ther is another one Smile | :)
Subdomain rewriting...

I was trying to make rewriting of subdomain url rewriting using HTTP module but with no success. (eg. http://subdomain.domain.com/default.aspx rewrite to http://www.domain.com/default.aspx?id=subdomain)

Whenever I try to rewrite full URL I get error telling me it’s not supported J. As Url rewriting is doing internal address rewriting it is normal to get that kind of error but how to accomplish that kind of thing in ASP.NET?

P.S. my dev machine is WinXP Pro (one possible and default web site) no DNS.
So I use hosts. file for host heading that site in combination with setting IIS web site host header names

Preky
GeneralForm Submission Pin
Steven A Bristol12-Apr-05 2:42
Steven A Bristol12-Apr-05 2:42 
QuestionWhy? Pin
Matt Berther10-Apr-05 5:31
Matt Berther10-Apr-05 5:31 
AnswerRe: Why? Pin
Emmanuel Ay10-Apr-05 7:10
Emmanuel Ay10-Apr-05 7:10 
GeneralRe: Why? Pin
Matt Berther10-Apr-05 12:08
Matt Berther10-Apr-05 12:08 
AnswerRe: Why? Pin
McGiv10-Apr-05 7:20
McGiv10-Apr-05 7:20 
GeneralRe: Why? Pin
Emmanuel Ay10-Apr-05 7:33
Emmanuel Ay10-Apr-05 7:33 
GeneralRe: Why? Pin
Wayne Theisinger22-Apr-05 6:10
sussWayne Theisinger22-Apr-05 6:10 
GeneralRe: Why? Pin
stixoffire13-Mar-07 0:33
stixoffire13-Mar-07 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.