Click here to Skip to main content
Click here to Skip to main content

URL cloaking with ASP.NET (C#)

By , 10 Apr 2005
Rate this:
Please Sign up or sign in to vote.

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:

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

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.

Comments and Discussions

 
QuestionIs there any way to catch 404 before IIS? Pinmemberxibeifeijian19-Mar-07 17:56 
AnswerRe: Is there any way to catch 404 before IIS? PinmemberAgenor20-Jun-10 23:20 
GeneralCool! This is exactly what I am looking for! PinmemberPetrander5-Sep-06 21:26 
GeneralURL rewriting problems? need help. PinmemberPreky15-Sep-05 22:52 
GeneralForm Submission PinmemberSteven Bristol12-Apr-05 2:42 
QuestionWhy? PinmemberMatt Berther10-Apr-05 5:31 
AnswerRe: Why? PinmemberEmmanuel Ay10-Apr-05 7:10 
GeneralRe: Why? PinmemberMatt Berther10-Apr-05 12:08 
AnswerRe: Why? PinmemberMcGiv10-Apr-05 7:20 
GeneralRe: Why? PinmemberEmmanuel Ay10-Apr-05 7:33 
GeneralRe: Why? PinsussWayne Theisinger22-Apr-05 6:10 
GeneralRe: Why? Pinmemberstixoffire13-Mar-07 0:33 

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

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

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