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&ProductID=$1" />
</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/3-Samsung940BW.aspx” will be rewritten as “~/ShowProduct.aspx?ProductName=Samsung940BW&ProductID=3”
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
<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!
My code is
**************
<!—->
***************
Is loading the same page two times,
In the second time loading , the values for Query string are changing,
please help me.
really nice article
Hi – great article, thanks! Could you please explain one thing in your example:
The syntax of the second regex rule doesn’t work for me in testing. Instead I have used: “~/Products/([^/.]+)/([^/.]+)\.aspx”. Am I missing something? Does your syntax not require something like “~/Products/Monitors/3-Samsung940BW.aspx” to match it? Thanks for your help!
Yes Eoghan the friendly URL should be like “~/Products/Monitors/3-Samsung940BW.aspx” for the regex mentioned. The product id (3) is also passed in the url since it maybe required. However if you dont require the product id in the URL , the regex rule can be modified as you have mentioned. I think I should mention it in the post to clear things up
Hi. I’m using the UrlRewriter module. A question about you solution for the google indexing problem. I’ve just put my site live and after checking in google’s webmaster tools, all my urls are returning 302 (for aspx pages) html, images etc return 200. The issue you described above says ‘Network Unreachable’ do you think it’s the same problem? Regards. Matt
Yes Matt, I was having the same problem. Have you tried to use the browser file? It solved the problem for me
Hi,
I read from UrlRewriter website that the rule is
but it didn’t run on my page.
Then I changed ^ to ~ :
and it worked.
Can you tell me if why the first rule didn’t worked?
Thanks.
Hi guys,
Has anyone figured how to set the status to 301 redirect with the URLRewrite.net component? The problem I’m facing is that I have many URL’s that are well ranked on google, and I would like to transfer that rank to the new friendly URLs generated by this component.
Someone knows how to do this? Thanks!
Hi Jesus,
I think that you might be able to use instead of .
I have the same problem as mentioned above, and the suggested fixes didn’t help me.
I found this page http://www.aspnetpro.com/articles/2009/03/asp200903jh_f/asp200903jh_f.asp where is mentioned near the bottom. So I have just uploaded a new web.config and I’m waiting to see if Google accepts the sitemap this time.
So maybe I should have read the user’s guide for URLrewritet.NET anyway
Best,
Johnny
Hi, can any one guide me, i am working on a jobs website, i am having multiple querystring in the url, like title=&location=&category=&experience= some query may be blank, how do i rewrite the url?
Hi, Nice article – I’m trying to implement this URL Rewriting but failing miserably at every turn…
My site, I’m trying to implement this solution:
Existing URL:
http://epiccruise.com/type_details.aspx?typeid=2
URL I want to replace with:
http://epiccruise.com/cruise-types/luxury-cruises/
I want to make the sub folder ‘cruise-types’ constant whenever type_details is there, and more importantly, i want the associated ‘TypeName’ that is related to the typeID to be displayed in the URL string.
I have absolutely no idea how to do this, would anyone be willing to show me how?
Thanks
Jez
Hi Jez
The rewrite URL will be as below
<rewrite url = "~/cruise-types/([^/.]+)\.aspx” to=”~/type_details.aspx?typename=$1″ />So you can use friendly urls like http://epiccruise.com/cruise-types/luxury-cruises.aspx
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 have URLs
1. http://localhost/categories.aspx?category=Mycat
2. http://localhost/subcategory.aspx?subcategory=NewMycat
I want these URls to be like this
1. http://localhost/Mycat/
2. http://localhost/NewMycat/
Can anybody help me in this thing?
Please help
I use procedure to create a virtual url,it return a string,example : “../FAQs/virtualurl-3″,and in webconfig i set : ,in this case,$2=3.So it work correct on local but when i uploaded on server it not work.
Help me,plz
I use procedure to create a virtual url,it return a string,example : “../FAQs/virtualurl-3″,and in webconfig i set : rewrite url=”../FAQs/(.+)-(.+)” to=”../manual/FAQ.aspx?id=$2″ ,in this case,$2=3.So it work correct on local but when i uploaded on server it not work.
Help me,plz
I apologize if this forum was not meant for this type of question, but I am trying to setup a somewhat simple rule in urlwriter that I just cant figure out. I am trying to execute a redirect only if the url does not contain index.html. so I tried
I have tried several other things, but nothing works, please help.
Hi..
I have added the same file in App_Browser but still i cant c the changes. I check my pages by this site http://analyze.websiteoptimization.com. It shows that page not found. So how much time it take to get all the pages after you add this file?
Hi All
i am using Intelligencia.UrlRewriter.dll for Url rewriting..
In web.config this works fine
http://localhost:3878/products.aspx/books
My problem is
——————-
http://localhost:3878/products/books
This type is working in my local server but when i upload in FTP
it is not working
http://www.cminfoline.cmlinks.com/products/books—not working
Please help me with this….
Thanks in Advance
hi,
i am having problem with masterpages.
can anyone having same problem?
its not able to find the refrenced javascript & css file
thanks
Paagnesh
I have an interview later today. ,
Wonderful, I’ve been looking for something simple, but i cannot get it to work!!! I uploaded the .dll to bin, added the web.config additions and I get no errors or no results using .NET 2.0/on Net. Sol. host. The URL doesn’t change if I use query parameters, or just a basic .aspx page. I even tried urlMappings with no results. It must be a config setting. So sad, can you please help?
view web.config here
http://www.adinc.us.com/webconfig.txt
Hello , great input,
i have added the reference to the Intelligencia.UrlRewriter release dll, added the web config for UrlRewriter but the url isn’t changing
here are the rules i used
i want to achieve the following
http://gourmetshop.gotdns.com:8090/Ξηροί-καρποί-c1/
instead of
http://gourmetshop.gotdns.com:8090/catalog.aspx?categoryID=1
any thoughts why the url rewriter doesn’t work properly?
i use visual studio pro for the developing
Thank you
here is the rule which i used
rewrite url=”^.*-c([0-9]+)/?$” to=”~/Catalog.aspx?categoryID=$1″ processing=”stop”
this is the error i get
Message 1 Could not find schema information for the element ‘rewriter’. C:\inetpub\wwwroot\gourmetshop\gourmetshop\web.config 24 4 C:\…\gourmetshop\
How to use querystring with rewritten url?
e.g. http://localhost/products/DVD.aspx?act=1
how to access act querystring variable in page load?