URL Rewriting in ASP.NET using URLRewriter.Net
Friendly URLs have become necessary to obtain better rankings in Search engines. The improvement in rankings may not be huge but because of the burgeoning of websites in almost every niche we need to use every single advantage that we can get. Friendly URLs are URLS without a query sting or without the “?” character. Friendly URLs have a URL structure which look like static pages instead of dynamic pages.
Unfriendly URL : “www.MyShop.com/ShowProduct.aspx?ProductID=1
Friendly URL : “www.MyShop.com/Monitors/Samsung-Syncmaster.aspx
The absence of a good, built in URL rewriting solution in ASP.NET makes it difficut to implement friendly URLs in dynamic pages. The only type of URL rewriting available in ASP.NET can be used only for small websites with a few pages since we need to have a rule for each page. The following is an example of URL rewriting which can be done in the web.config file
<system.web>
<urlMappings enabled="true">
<add
url="~/Clubs/Computer Club.aspx"
mappedUrl="~/ShowPage.aspx?PageID=1" />
<add
url="~/Clubs/Eco Club.aspx"
mappedUrl="~/ShowPage.aspx?PageID=2" />
</urlMappings >
</system.web>
You can easily see that a website with even a fairly large number of pages can become difficult to maintain. Adding or deleting of pages will also require the web.cong file to be modified. This is a tedious solution and certainly not suitable for medium to large dynamic sites.
The guys at URLRewriter.NET have made a very useful URL Rewriting component . The best thing about it is that it is very simple to implement and it works in a shared hosting environment. All you have to do is download the dll file and copy it to your bin directory. Add a few lines in your web.config file as follows.
<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
</system.web>
Now you will have to define the rules in your web.config file
An example of a rule is as follows
<rewriter>
<rewrite url = "~/Products/([^/.]+)\.aspx” to=”~/ListProducts.aspx?Category=$1″ />
<rewrite url = “~/Products/(.+)/([0-9]+)-(.+).aspx” to=”~/ShowProduct.aspx?ProductName=$2″ />
</rewriter>
According to the the first rule “~/Products/Monitors.aspx” will be rewritten as “~/ListProducts.aspx?Category=Monitors”
According to the second rule “~/Products/Monitors/Samsung940BW.aspx” will be rewritten as “~/ShowProduct.aspx?ProductName=Samsung940BW”
Thus now you can use the friendly URLS instead of the URLs with query strings. It’s simple as that !
After using URLRewrite.NET , I had noticed a major bug. The bug is not in the component but with ASP.NET URL rewriting itself. All the pages using URL Rewriting do not get indexed by Google, this happens for both the built in URL rewriting and URLRewriter.NET component. Google bots cannot crawl pages which use Rewriting. It gives an error “Network unreachable” . This is a major problem for websites which depend on search engine traffic and almost all websites do.
After searching around a lot , I found a solution . The solution is to create a file name “genericmozilla5.browser” under the “App_Browsers” Directory in your root directory. Type in the following in the file
<browsers>
<browser id="GenericMozilla5" parentID="Mozilla">
<identification>
<userAgent match="Mozilla/5\.(?'minor'\d+).*[C|c]ompatible; ?(?’browser’.+); ?\+?(http://.+)\)” />
</identification>
<capabilities>
<capability name=”majorversion” value=”5″ />
<capability name=”minorversion” value=”${minor}” />
<capability name=”browser” value=”${browser}” />
<capability name=”Version” value=”5.${minor}” />
<capability name=”activexcontrols” value=”true” />
<capability name=”backgroundsounds” value=”true” />
<capability name=”cookies” value=”true” />
<capability name=”css1″ value=”true” />
<capability name=”css2″ value=”true” />
<capability name=”ecmascriptversion” value=”1.2″ />
<capability name=”frames” value=”true” />
<capability name=”javaapplets” value=”true” />
<capability name=”javascript” value=”true” />
<capability name=”jscriptversion” value=”5.0″ />
<capability name=”supportsCallback” value=”true” />
<capability name=”supportsFileUpload” value=”true” />
<capability name=”supportsMultilineTextBoxDisplay” value=”true” />
<capability name=”supportsMaintainScrollPositionOnPostback” value=”true” />
<capability name=”supportsVCard” value=”true” />
<capability name=”supportsXmlHttp” value=”true” />
<capability name=”tables” value=”true” />
<capability name=”vbscript” value=”true” />
<capability name=”w3cdomversion” value=”1.0″ />
<capability name=”xml” value=”true” />
<capability name=”tagwriter” value=”System.Web.UI.HtmlTextWriter” />
</capabilities>
</browser>
</browsers>
This solved the problem for me.
Update :
Also check my post on fixing postbacks while using URLRewriter.net
I’ve used this one. It’s good tool!
What does that browser file do to help out google bots in indexing url rewritten sites and where did you find it?
Hi
I have 2 pages default.aspx and show.aspx
On Default.aspx there are links to different blog posts,which are displayed on show.aspx
On show.aspx : Year and/or Month and/or Day are passed and then posts are displayed
: To show particular post that postId is passed to show.aspx
To display posts of year 2006 :
Actual URL : http://localhost/UrlRewrite/show.aspx?Year=2008
URL Should Look Like : http://localhost/UrlRewrite/2008/
To display posts of year 2006 and month August:
Actual URL : http://localhost/UrlRewrite/show.aspx?Year=2008&Month=08
URL Should Look Like : http://localhost/UrlRewrite/2008/08/
To display posts of year 2006 and month August and day 16:
Actual URL : http://localhost/UrlRewrite/show.aspx?Year=2008&Month=08&day=16
URL Should Look Like : http://localhost/UrlRewrite/2008/08/16/
To show post having postId 25:
Actual URL : http://localhost/UrlRewrite/show.aspx?postId=25
URL Should Look Like : http://localhost/UrlRewrite/2008/08/16/Post Title.aspx
How to do it?
Please help.
Hi
I am using following 3 rules :
If I comment 3rd rule, then first 2 rules are working fine.
But if I uncomment 3rd rule, then
all 3 rules are working like 3rd rule.
I have 2 pages default and products on root.
I have following links on default:
YYYYMMDD
YYYYMM
YYYY
What change should I make?
What am I missing?
According to the the first rule “~/Products/Monitors.aspx” will be rewritten as “~/ListProducts.aspx?Category=Monitors”
According to the second rule “~/Products/Monitors/Samsung940BW.aspx” will be rewritten as “~/ShowProduct.aspx?ProductName=Samsung940BW”
Shouldn’t it take the url with the query string and rewrite it to one without a query string. Instead of taking a non-query string url and rewriting it to a query string url
How do I check 100% if my rewritten URL’s are being indexed by search engines?
I’m using this module but it seems that I’m being indexed correctly, maybe I’ll give the browser file a show and see if it helps out my stats.
Also how many do you think is to may URL rewrites to be loaded into the web.config?
Thanks.
Very cool, very informative article! Thanks for sharing it. I’m giving this a test run soon and will look to implement into a web application I’m building.
@Jon The friendly URLs are visible to everyone , but your page “ShowProduct.aspx” requires a query string , so the URL Rewriter component changes your friendly URL to include query strings which will be passed to the “ShowProduct.aspx” page
@R The best way to check if URLs are being indexed would be to submit a sitemap to google and check if there are any errors. Usually the sitemap is checked for errors within 24 hrs
@Martin Thannk you for finding it informative. Hope you too can successfully use it
Strange, i’ve been using this component for quite some time and my re-written pages get indexed just fine in Google and other indexes without any special consideration.
Were you just noticing that you were getting errors in your sitemap submissions?
@Sharbel I think many people have faced ASP.NET’s URL rewriting bug. Even Matt Cutts had discussed it in his blog post here http://www.mattcutts.com/blog/asp-net-2-and-url-rewriting-sometimes-harmful/
, but it is also good to keep a close watch as in one of my old websites quite a few pages were not indexed by google and I noticed it only after a year
As I have said earlier it’s not only the URLRewriter.Net component that has the problem but even the built in declarative URL rewriting done in the web.config file . However as you have not had any problem, I guess you are lucky
I am using this on one of our sites and I was wondering where you were seeing the error, I am assuming this was in the logs, but if that is not the case. Then I would be curious as to where you saw it.
@irish If you are using google webmaster , it can show you the errors there . I also checked in google search using the command “site:www.mydomainname” . It showed me that my pages which used URL rewriting were not indexed .
[...] week I had done a post on how to rewrite URLs using the URLRewriter.Net component. You can read it here . Another issue that comes up while using URL rewriting in ASP.NET is the problem of postbacks. [...]
There is a little problem with the urlRewritingNet and web.Sitemap. The SiteMapPath wouldn’t work because the web.Sitemap won’t contain the specific URL.
Let’s say I have page ~/photos.aspx?albumid=10&lg=en rewrited to ~/en/photos/album/10. What I need is to get the URI ~/photos.aspx?albumid=10&lg=en to make the SiteMapPath working. Does anyone know how to solve this?
Thanks
I was shocked to see the claim that the urlrewritter pages aren’t indexed. I built a simple little cms solution that I’ve used on 30+ clients sites - and all the pages are using URLRewriting.NET - but all of the pages HAVE been indexed by google.
– Just Noticed Diference –
AAHH - there may be some confusion (at least on my part) - I am using URLRewritING.net not the URLRewrite.net (-ING). It seems to work the same as above - and is indexed by google!
Google recently said that URL rewriting is not that important. But I still think it´s nice for the visitior to have a more friendly looking URL.
Source:
http://googlewebmastercentral.blogspot.com/2008/09/dynamic-urls-vs-static-urls.html
How does the url re-writer interact or behave with the site map? In using sitemaps, I’ve found the url must match exactly in the site map - and I wondered if this has any implications when using the url re-writer.
I’ve tried to solve the bostback issue by modifying the GetUrl() method of the Form control you can find in the source code of the project.
Now it seems to work fine.
If you are interested in my solution please ask me, i don’t want to bother you by writing code if not requested
Cheers
hello
i didnt understand where to put the
in the web.config it gaive me errors like this:
Message 12 Could not find schema information for the attribute ‘to’. C:\Documents and Settings\admin\My Documents\Visual Studio 2008\WebSites\urw\web.config 31 48 C:\…\urw\
it is me again
it is working now great
thanks
Hi,
Thanks for sharing such a wonderful code. But we are facing a problem of cookies which seems to be not working along with URL rewriting.
We have done lot of coding in cookies and there is no choice of eliminating cookies also we need to have URL Rewriting with the site.
Can you please guide us on this, there would be several people around facing same problem.
Thanks in advance.
Anil
@Anil I didn’t have any problem using cookies with URL rewriting. I am using cookies and URL rewriting in one of my sites successfully.
Hi…to all.I’m searched a lot..for url rewriting.But I’m failed to get it.What is my requirement is, suppose we have an url
http://localhost/urlfeet/products.aspx?id=203
It must me like this
http://localhost/urlfeet/products/203/
I want like this .Any body is there to help.I dont need .aspx at the end of url.Please replay me if any body knows.
After you install URLRewriter.net as mentioned in the post above, you can use the following rule
Quote
<rewriter>
<rewrite url = "~/Products/([^/.]+)\.aspx” to=”~/Products.aspx?id=$1″ />
</rewriter>
However the friendly URl should be something like this
http://localhost/urlfeet/products/203.aspx
Hi Vihutuo ,
Thanks for u r reply.I already done url like this http://localhost/urlfeet/products/203.aspx
I dont need .aspx at end.What I have to do
Hi all!
I use urlrewrite.net
while i using config then have a problem!
test: http://localhost/Products/Item then OK
test: http://localhost/ the have a problem -> error: http 404 - File not found!
help me
thanks you!