Click here to Skip to main content
13,834,944 members
Click here to Skip to main content
Add your own
alternative version


58 bookmarked
Posted 10 Dec 2005

URL rewrite for .NET 2.0

, 10 Dec 2005
Rate this:
Please Sign up or sign in to vote.
Simple URL rewrite for .NET 2.0.


This is another article that describes the different solutions used to build I wrote this to get started with writing the technical descriptions about how we made the solution and to share our ideas with the community. The primary article on CodeProject can be read from here.

For the project we needed a simple way to rewrite the URLs to get rid of the nasty queries like the ID etc. When we first started the project we wrote a system where we only needed a single query to handle most of the requests. Check out this one.

Therefore, it was a very easy job to make a URL rewrite solution, so the URL would look like this:

The solution

We inserted a begin request handler in the global.asax file in the root directory. This handler checked to see if there existed a file with the name requested. In that case, I would not do anything. For example, a request like this: It would check to see if there existsed a file with that name in the root directory. If not it would rewrite the query to:

This is only rewritten on the server and the client still thinks that it uses the first URL. This is a nice thing because it still looks pretty in the browser. But it has one problem. The browser gets confused because all the relative paths are now incorrect. Therefore, you need to define the base path in HTML header like this:

        <base href="">

Since our project is an open source project the base path changes from system to system so we wrote a little script to let the server figure out the correct path:

[Editor comment: Line breaks used to avoid scrolling.]

string basepath = 
  + "://" + httpPage.Request.ServerVariables["SERVER_NAME"] 
  + ":" + httpPage.Request.ServerVariables["SERVER_PORT"] 
  + httpPage.Request.ApplicationPath.TrimEnd('/')+ "/";

This makes it a lot more dynamic with websites that have more than one domain associated.

Since the .NET Framework on IIS only handles certain extensions like .aspx you have to end the URL with aspx.

The global.asax file

I am not satisfied with the code yet. It looks to me that there should be a nicer or a more cleaner way to do this. I haven't found it yet though. We have tested the code for a while now and it works even with virtual paths.

The code to be placed in the global.asax file is this:

[Editor comment: Line breaks used to avoid scrolling.]

void Application_BeginRequest(object sender, EventArgs e) { 

    System.Web.HttpContext httpContext = HttpContext.Current;
    String currentURL = httpContext.Request.Path.ToLower(); 
    string processPath = 
    //Creates the physical path on the server 
    string physicalPath = httpContext.Server.MapPath(
                  currentURL.Substring(currentURL.LastIndexOf("/") + 1));
    //checks to see if the file does not exsists.
    if (!System.IO.File.Exists(physicalPath))
        string queryString = 
        string defaultPage = "~/default.aspx?process=";
        if (processPath.EndsWith(".aspx"))
            processPath = processPath.Substring(0, 
                            processPath.Length - ".aspx".Length); 
        // Rewrites the path
                      defaultPage + processPath + "&" + queryString);


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

Web Developer
Denmark Denmark
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionNice article Pin
Navin Pandit13-Jun-12 21:00
memberNavin Pandit13-Jun-12 21:00 
GeneralImage path problem Pin
anrorathod22-Aug-07 2:30
memberanrorathod22-Aug-07 2:30 
GeneralRe: Image path problem Pin
Seema Gosain21-Nov-08 0:42
memberSeema Gosain21-Nov-08 0:42 
GeneralRewriting a folder Pin
Evan Camilleri13-Feb-07 6:30
memberEvan Camilleri13-Feb-07 6:30 
GeneralRe: Rewriting a folder Pin
coverboy28-Feb-07 2:11
membercoverboy28-Feb-07 2:11 
GeneralRe: Rewriting a folder Pin
Evan Camilleri28-Feb-07 6:04
memberEvan Camilleri28-Feb-07 6:04 
GeneralRe: Rewriting a folder Pin
coverboy1-Mar-07 14:43
membercoverboy1-Mar-07 14:43 
GeneralRe: Rewriting a folder Pin
Evan Camilleri2-Mar-07 0:33
memberEvan Camilleri2-Mar-07 0:33 
GeneralRe: Rewriting a folder Pin
N a v a n e e t h14-May-07 0:20
memberN a v a n e e t h14-May-07 0:20 
GeneralBetter version... Pin
Sire40412-Dec-05 4:29
memberSire40412-Dec-05 4:29 
GeneralRe: Better version... Pin
peterhansen212-Dec-05 4:39
memberpeterhansen212-Dec-05 4:39 
GeneralRe: Better version... Pin
Sire40412-Dec-05 5:10
memberSire40412-Dec-05 5:10 
GeneralSuggestion Pin
Wade Reynolds12-Dec-05 4:06
memberWade Reynolds12-Dec-05 4:06 
GeneralRe: Suggestion Pin
peterhansen212-Dec-05 4:49
memberpeterhansen212-Dec-05 4:49 
GeneralRe: Suggestion Pin
Richard Priddy8-Feb-07 5:17
memberRichard Priddy8-Feb-07 5:17 
Generalnice idea :) Pin
Francesco Aruta11-Dec-05 3:38
professionalFrancesco Aruta11-Dec-05 3:38 
GeneralFeedback Pin
peterhansen211-Dec-05 7:17
memberpeterhansen211-Dec-05 7:17 

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 | Cookies | Terms of Use | Mobile
Web02 | 2.8.190114.1 | Last Updated 10 Dec 2005
Article Copyright 2005 by peterhansen2
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid