ASP.NET Hosting

Solving URL rewriting problems with themes and trailing slashes

In a comment to an old post of mine about URL rewriting, a visitor named Tim has just asked how to solve a problem he was facing with ASP.NET themes and rewriting. The original post was addressing the main problems by using an overload of the RewritePath method introduced by .NET 2. Yet, a simple problem still existed: whenever URL rewriting is used with a URL like ~/somepath/ the theme gets broken because the path to the CSS files and other themed resources (like images) are wrong. The problem here is the trailing slash, which confuses the theme engine. A URL like ~/somepath works fine of course.

In fact, I recently noticed that I had the same problem with my own URLs on SharpToolbox/JavaToolbox and on a new site I'm working on. What I did to resolve this is to perform a (permanent) redirection to URLs without the ending "/". This is indeed what we wish to express: ~/somepath/ and ~/somepath are the same URLs.

Here is the regular expression I use to identify a problematic URL: ^(.*)/(\?.*)?$
I then redirect to: $1$2
You can notice that this expression also supports URLs with parameters.

If you use UrlRewritingNet.UrlRewrite you can use the following rule:
<add virtualUrl="^(.*)/(\?.*)?$" destinationUrl="$1$2" rewriteUrlParameter="ExcludeFromClientQueryString" redirectMode="Permanent" redirect="Domain" />

If you use my old HTTP module, you should be able to use:
<add targetUrl="^(.*)/(\?.*)?$" destinationUrl="$1$2" permanent="true" />

11 Comments

  • hmmm. I didi use URL rewriting and had to keep all the URLs in the same level because of this problem. NOw I will check them and may be create some more rules with nested URLS

    Thanks
    This was really helpful

  • Hi

    I am using UrlRewritingNet.UrlRewrite. But i did not know how to used and I am also new in Regular Expression. I want to rewrite the URL. Can I Rewrite URL from one website to another website ? means I have a one site www.mywebsite1.com and another is www.mywebsite2.com. Now somebody visit www.mywebsite1.com but all the content is coming from www.mywebsite2.com but i do not want to change the URL 1 to 2. it display as it is means it looks like contents are coming from www.mywebsite1.com not www.mywebsite2.com.

    If it is possible then what should I have to do ?? give me explaination with examples..

    Thank you

  • Jwalin, I don't think it's possible with rewriting. What you need to do instead is dealing with the DNS settings for your sites and declare mywebsite1.com as an alias for mywebsite2.com.

  • Bless you kind sir - I was pulling out my hair on this until Google sent me your way, it works perfectly. Thank you.

  • Hi,
    I'm encountering a problem with ASP.NET AJAX Page Methods not working with UrlRewriting.NET and found this post while searching on it. Any idea about it ?

  • Hi Mohamed,

    Your best bet is to visit UrlRewriting.Net's forum : http://groups.google.com/group/UrlRewritingNet

    Fabrice

  • Hi Fabrice, do you know why this could be a problem on IIS over Win2000 and not WinXP? Has this been solved in any other way?

    I'd like to be able to use it the other way around... :)

  • It really helps when it comes to trailing slashes but as soon as i implement this rule my homepage will stop working

    i have a rule even sayin if ~/ go to ~/ or ~/index.asp

    but when i add this i have an error in the way it redirects the page...

    so if i go to www.mydomain.com i have an error but if i go to www.mydomain.com/index.asp it works

  • George, I don't have this problem. You shouldn't need any special rule for the home page. I'd remove the extra rules you've added for this.

    Also, it's not possible to help you without knowing what the error you get is.

  • when I use url rewrite, my css and images does not work any more, how can I fix this?

  • HI,
    I am Using UrlRewritingNet.UrlRewrite and it is working fine.

    But I have One Major Problem in that when I am Using this Rewriting Rule in My Application the Button Click is Not Firing in whole Application.

    so please reply back to me with proper solution is there any setting we have to do for that Click Event.

    reply me As soon as possible.

    regards
    Hemant

Comments have been disabled for this content.