Fabrice's weblog

Tools and Source

News


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

.NET jobs

Emplois .NET

The views expressed on this weblog are mine alone and do not necessarily reflect the views of my employer. The content of this weblog is independent from Microsoft or any other company. transatlantys hot news

Contact

Me

Others

Selected content

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

Ikke said:

To solve this problem of the site not getting its themes, I don't change the url at all. I add a <base> tag in the header (for each page), i.e. if your domain = "www.yourwebsite.com" then you add:

<base href="www.yourwebsite.com/" />

Since you're all not metioning this, Is there a reason not to do this?

# July 17, 2008 9:49 AM

Manir said:

Hi Fabrice,

I am using simple technique to implement URL rewriting. For example instead of uisng www.mysite.com/category.aspx I am using www.mysite.com/.../cat_2-Category-Name. From the category.aspx page I decode Cat Id and render the page. I am also using Master Pages and asp.net themes. Becuase of the trailing slases ("/cat_2-Category-Name"), image are not displayed which using SkinId. Except these trailing slash - everything looks perfect. Please let me know if you have any solution for this.

Please help me - I am stuck.

Thanks,

Manir

# September 20, 2008 4:57 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)