Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

Tuneo

ASP.NET Hosting transatlantys

Contact

Me

Others

Selected content

Archives

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" />

Comments

vikram said:

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
# October 20, 2006 1:05 AM

Jwalin Khatri said:

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

# December 7, 2006 12:56 PM

Fabrice Marguerie said:

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.

# December 7, 2006 6:29 PM

Carson said:

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

# December 14, 2006 6:00 PM

Mohamed Meligy said:

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 ?

# December 23, 2006 6:14 PM

Fabrice Marguerie said:

Hi Mohamed,

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

Fabrice

# December 24, 2006 6:33 AM

Ezequiel said:

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... :)

# January 31, 2008 1:21 AM

George said:

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

# April 28, 2009 6:24 AM

Fabrice Marguerie said:

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.

# April 28, 2009 9:17 AM

George Garchagudashvili said:

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

# September 30, 2009 2:24 PM

Hemant said:

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

# February 1, 2010 12:45 AM