Click here to Skip to main content
Click here to Skip to main content
Go to top

IIS & ASP.Net URL Rewriting

, 4 Sep 2011
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
Founder
United Kingdom United Kingdom
Hi there, so you're reading this? I probably ought to tell you something helpful then...
 
Okay well, I'm 17 from London, UK and I have been programming since I was 8 years old. You'll see from my profile I've dabbled in a lot of different areas and I do know what I'm talking about so as they say "gimme some respect yeah?" Smile | :) I've done GCSE's (got 7A*'s, 2A's and a B in French) and am now studying for my AS-Levels (Math, Further Maths, Physics and Chemistry). My main aim is to go to university to study electronic engineering and then work in robotics (preferably humanoid but most of the field is interesting).
 
Thanks for reading,
Edward Nutting
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 4 Great tips and I was just looking at... PinmemberKenBonny6-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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 4 Sep 2011
Article Copyright 2011 by Ed Nutting
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid