Click here to Skip to main content
11,637,415 members (62,777 online)
Click here to Skip to main content

IIS & ASP.Net URL Rewriting

, 4 Sep 2011 CPOL 13K 4
Rate this:
Please Sign up or sign in to vote.
IIS_ASPNet_URLRewriting
A common thing to want to do with websites is implement URL Rewriting and there are some fabulous articles on it - I recently used this one on MSDN[^]. This example does it through ASP.NET HttpModules and works very nicely. However, as pointed out at the very bottom of the article, many URL Rewriting implementations require the developer to also create and maintain a folder for every possible URL a user could want to access. This is a huge effort and maintenance job for any site of reasonable size and so, I set out to find the solution. After much searching, I had not found an adequate answer - most people said to use ISAPI or use one of the free systems on the internet - not what I wanted or needed. So I turned to my father who had done some things like this before and, after a bit of searching in odd places, we found an interesting and seemingly un-posted solution.

The solution to maintaining folders with blank aspx files in them is to NOT create the folders at all. Instead, allow IIS to throw HTTP 404 errors. This is useful because IIS returns a default 404 page with error message as standard, but you can reset this 404 page to be any page you like. Thus the second half of the solution is to set the Http 404 Error page to an ASPX page in your website. The original URL can be accessed through the Application.Request.RawURL in your HTTP module and then you can rewrite the URL as explained in the article I mentioned.

It is a relatively simple solution to implement. Open IIS and go to the website you wish to implement URL redirecting on and go in to the Properties window (Right Click -> Properties). Select the Custom Errors Tab then scroll through the list until you find HTTP 404 (ignore any sub error codes). Select HTTP 404 and click the Edit button at the bottom of the window. This open the Edit window. Set Message Type to URL and then enter the site root relative URL of the aspx page you wish to redirect to (normally your main Default.aspx). Save that and you're done! Just implement URL Rewriting as explained in the article on MSDN (see start of Tip/Trick).

For those wishing to have even more detailed (or general) control, HTTP Errors can also be set for the entire server or even individual folders in your website. You could therefore apply URL Rewriting to just one folder in your site or different rewriting strategies to different folders but it is beyond the scope of this Tip/Trick to explain in detail how the first step is to perform the above instructions the same, but Right Click on the folder/server you wish to set for.

I have found this an extremely easy, quick to implement and useful solution.

I would also like to add that as Regular Expression play such a large role in URL Rewriting I have found the following sites to be incredibly helpful:

http://gskinner.com/RegExr/[^] : Online Regex Tester and builder with explainations of the different symbols and a few examples. Very useful though takes a little getting used to. Also, remeber to use Ctrl + C, Ctrl + V and Ctrl + X for copy, paste and cut respectively (frustrates me every time I try to use Shift + Insert etc. and it doesn't work!)

http://regexlib.com/[^] - A library of thousands of free Regular Expressions - while often not exactly what you want, they're a good place to start.

Ed Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ed Nutting
Student
United Kingdom United Kingdom
Hi,

I am Edward Nutting, born March 1996 and I am currently an Electrical and Electronic Engineering undergraduate at the University of Bristol, UK. I've been self-teaching programming since I was eight years old and have dabbled in many different areas from web development (where I had my own advertising startup) to low level operating system stuff (my current project Fling OS).

The articles and code on this profile are some of my early work (from when I was aged about 13 to 16) but despite my young age, it has been well received by CodeProject users. My more recent work has been for companies or on projects hosted on GitHub, CodePlex or BitBucket.

I keep an eye on many things techy and am open to any and all ideas, so please feel free to contact me if you think I can help! Smile | :)

Cheers,
Ed Nutting

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 4 Great tips and I was just looking at... Pin
KenBonny6-Sep-11 20:29
memberKenBonny6-Sep-11 20:29 

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 | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 4 Sep 2011
Article Copyright 2011 by Ed Nutting
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid