ASP.NET Hosting

ASP.NET HTTP module for URL redirections

For my own web sites, I needed to perform multiple URL rewritings, both in order to get short URLs and to charm Google and friends. You can see this at work for the SharpToolbox for example.
I improved Fritz Onion's HTTP Module, which performs redirects based on entries in your web.config file. It is regular expression-based, and is an easy way to perform automatic redirection for one set of URLs to another in your site.

I improved performance by keeping Regex instances, and added support for the ~ character.

Here is what the config file can look like:

<redirections type="Madgeek.Web.ConfigRedirections, Madgeek.RedirectModule">
 
<add ignoreCase="true"
   
targetUrl="^~/FalseTarget.aspx"
   
destinationUrl="~/RealTarget.aspx"
/>
 
<add permanent="true"
   
targetUrl="^~/2ndFalseTarget.aspx"
   
destinationUrl="~/RealTarget.aspx"
/>
 
<add
   
targetUrl="^~/(Author|Category|Tool)([A-Za-z0\d]{8}-?[A-Za-z\d]{4}-?[A-Za-z\d]{4}-?[A-Za-z\d]{4}-?[A-Za-z\d]{12}).aspx$"
   
destinationUrl="~/Pages/$1.aspx?$1=$2"
/>
  <add
   
targetUrl="^~/SomeDir/(.*).aspx\??(.*)"
   
destinationUrl="~/Pages/$1/Default.aspx?$2"
/>
</redirections>

I joined a small demo to the source code. Download.

Update: 2004/11/19 - added support for ignoring case. See the ignoreCase attribute.
Update: 2005/05/12 - works with .NET 2.0, you just have to replace the calls to ConfigurationSettings.GetConfig() by calls to the new ConfigurationManager.GetSection()

31 Comments

  • Very nice work Fabrice!

  • Mark,



    What I do is write a mapping XML file in combo with a HttpModule that will read the request and look up the real path through XXXPath ;)



    That gives me a lot of leverage because I don't have to manually add new pages to a config file. It's being generated everytime a page gets saved/updated.

  • You can, but this requires having all HTTP requests redirected to your application.
    By default, only "standard" ASP.NET requests are handled by ASP.NET applications. This means that you can process, an so redirect, only requests to .aspx, .asmx and .ashx documents. In order to get all requests redirecte to your application, you need a special configuration to be set in IIS. This may be difficult to get from your hoster, so you'd better check if they allow you to do that. Ask them if they can map .* to your ASP.NET application.

  • To map all the requests to asp.net engine is a solution but might be a little difficult and you can have problems when deploying, I was looking for a solution because I'm working right now on building a asp.net blog and I'm using url rewriter. The problem is I don't want to create dummy directories to contain empty default.aspx pages. From IIS right click on your virtual directory then go to Custom Errors and edit properties for 404 error code, change the Message Type drop down to URL and add an aspx page instead (something like this: /Blog/404error.aspx). This means every missing pages will end up by calling 404error.aspx. At this moement if you access a subdirectory that does not exists the IIS should try execute 404error.aspx page. At this point you can see the original request from the QueryString and use it in url rewriter. Sorry for my bad english.

  • Hi - this is great- but i'm a newbie to this world - i've got a vb.net asp.net app - i can see that this is a c# app - can i integrate this with my vb.net app?

    thanks

  • Nathan, everything that is written in C# can be used as-is in VB.NET, and vice-versa. C# and VB.NET are two languages that work on the same platform and runtime (.NET).

  • I&#39;ve been using this for a while now and it has been excellent. However I have just come across a problem. I need to use both URL rewriting and redirects (to redirect from &quot;moved permanently&quot; pages using an HTTP 301). However both Http module I need to use for this want to use &lt;configuration&gt;&lt;appSettings&gt;&lt;redirections&gt;&lt;/redirections&gt;&lt;/appSettings&gt;&lt;/configuration&gt; in my web.config, and of course .NET won&#39;t allow this. Is this section name specified in the module itself? If so, would it be possible to change the section name to something more specific to the module, like or something?

  • Dave, yes, feel free to do the change in the source code.

  • OK I've only just realised this module can do both URL rewriting AND 301 permanent redirects. I was trying to use two seperate http modules, when I could use this one for both. Duh.

  • Has anyone implimented this ??? if so this could save me looking into this as its once one of my lists to do !!!

  • Hi,
    well this URL redirections works well, I have to get the new url (dummy) to be created without hardcoding as did . i,e if in a page it was already there like Response.Redirect("~\search.aspx?ptype=hotel"). I need to rewrite this url to "~\search\hotel.aspx" and then redirect to that url. have anyone implemented this.
    Reply if done. thanks in advance

  • Lance, you should be able to use a rewriting or a redirection from /SiteRoot/Register/ to /SiteRoot/Register/Default.aspx in your configuration file.

  • This should be possible. All you need is to find the correct regular expression.

  • This works fine under the local asp.net server but i'm getting 404s when i deploy with IIS (5), i assume IIS is taking the www.mydomain.com/mypage/myparameter URL format and attempting to navigate straight to that directory.

    Has anyone else come up against this issue and found a workaround? Thanks.

  • Town, in IIS 5 there is an option to "Check File Exists" on the "Add/Edit Application Extension Mapping" screen. Make sure it's off.

  • Hi Fabrice, thanks for the reply. That setting looks to be a per-extension setting, which extension does it need to be set for? It's already off for .aspx files.

    Thanks.

  • If all you use is .aspx, then it looks like your problem comes from something else.
    If you use another extension or no extension (* or .*), then make sure the option is off for them too.

  • Hello,

    I using this URlRewriting Module to rewrite and to redirect...

    I'm using rules with redirectMode="Permanent" but I still have a 302 redirection. That is quite strange? Do you have any good example for 301 redirection?

    Thanks for your help!

  • Hi Emmanuel,

    There is no redirectMode attribute in this rewriting module. You should use permanent="true", as in the sample provided in this post.

    Fabrice

  • Umid, I don't think that this is any different than with any other version of ASP.NET. It doesn't look specific to IIS 7.

  • Hi Fabrice, thanks for your article, it let me know more about the redirect url knowledge. but i have problem when i use your demo to see the result, and i got the following problem, could you help me to solve it??
    "Parser Error Message: Could not load type 'RedirectModuleDemo.Default'." and
    Line 1: in default.aspx

    Thanks!

  • Hi Tisa, this error doesn't seem to be related to URL rewriting. Have you changed anything in the source code of the demo? Which version of Visual Studio are you using?

  • I haven't used VS 2003 for many years now, but I wonder why you copied the demo folder. I think you should instead leave the files all in one place, create an IIS application on the Demo folder, and then open the solution in VS and run the project from there.
    It looks to me as if you haven't built the project...

  • Hi Fabrice,

    Thanks for your reply. Now it is working when run your demo. But i don't see any redirect action. when i click FalseTarget.aspx link, it is not go to realTarget.aspx, do i have something need to set before run this program? is it relate to the programming language c#??

  • > "when i click FalseTarget.aspx link, it is not go to realTarget.aspx"

    What happens instead? Do you get an error? Without more information, I'm not able to help you.

  • Hi Fabrice,

    It doesn't have any error. it just show HTTP 404 error code. I think it only mean the file cannot be found.

    Thanks!

  • Tisa, it seems that you need to make sure that "Check that file exists" is unchecked for the .aspx extension mapping in IIS.

  • Thanks. After an insuccessful migration to a new CMS, I need to revert back to the static web site, but Google already indexed my new files, so thanks to you component I've been able to setup 301 redirect in a couple of seconds.

  • in My Application Button Click Event is not Firing please anybody tell me the solution for it.
    HOw My Button Click worked in My Application
    Rest All thing are working fine
    Please send me solution
    I am waiting for your reply

    Regards
    Hemant
    hemant.tawri@techendeavour.com

  • It depends on the component you use for doing the rewriting, but in any case you should search for problems with postbacks when using URL rewriting.

    Here are articles that deal with this:
    http://msdn.microsoft.com/en-us/library/ms972974.aspx
    http://weblogs.asp.net/jezell/archive/2004/03/15/90045.aspx

  • Hi All,

    Im using the component for dynamic rules derived from database. However once application is restarted I get 404 error and when refresh the page it loads fine. Does anyone happened to know anything about that?

    thanks

Comments have been disabled for this content.