Click here to Skip to main content
13,043,092 members (61,779 online)
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 10 Apr 2005

URL cloaking with ASP.NET (C#)

, 10 Apr 2005
Rate this:
Please Sign up or sign in to vote.
A short article on how to replace your long and ugly querystring URLs with clean and simple URLs in ASP.NET.


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:

or 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:


This is where URL cloaking comes in handy.


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:

// 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.


  • 2005-04-10: Created


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


About the Author

Emmanuel Ay
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.

You may also be interested in...

Comments and Discussions

QuestionIs there any way to catch 404 before IIS? Pin
xibeifeijian19-Mar-07 17:56
memberxibeifeijian19-Mar-07 17:56 
AnswerRe: Is there any way to catch 404 before IIS? Pin
Agenor20-Jun-10 23:20
memberAgenor20-Jun-10 23:20 
GeneralCool! This is exactly what I am looking for! Pin
Petrander5-Sep-06 21:26
memberPetrander5-Sep-06 21:26 
GeneralURL rewriting problems? need help. Pin
Preky15-Sep-05 22:52
memberPreky15-Sep-05 22:52 
GeneralForm Submission Pin
Steven Bristol12-Apr-05 2:42
memberSteven Bristol12-Apr-05 2:42 
QuestionWhy? Pin
Matt Berther10-Apr-05 5:31
memberMatt Berther10-Apr-05 5:31 
AnswerRe: Why? Pin
Emmanuel Ay10-Apr-05 7:10
memberEmmanuel Ay10-Apr-05 7:10 
GeneralRe: Why? Pin
Matt Berther10-Apr-05 12:08
memberMatt Berther10-Apr-05 12:08 
AnswerRe: Why? Pin
McGiv10-Apr-05 7:20
memberMcGiv10-Apr-05 7:20 
GeneralRe: Why? Pin
Emmanuel Ay10-Apr-05 7:33
memberEmmanuel 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
memberstixoffire13-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 10 Apr 2005
Article Copyright 2005 by Emmanuel Ay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid