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
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:
] : 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!)
] - A library of thousands of free Regular Expressions - while often not exactly what you want, they're a good place to start.