In the past I linked to an article on URL rewriting and how to do it. This past weekend I finally modified my personal web site to use URL Rewriting.
I tried a couple different methods of URL Rewriting and finally settled in with http://www.urlrewriting.net as the documentation was good and the implementation fit my needs.
Essentially all that was required was to drop the urlrewriting.dll into my bin directory (adding it as a reference for my website) and adding rewriting rules to my web.config.
The power is in adding rewriting rules to the web.config.
You create the functionality you want as query string variables, and then create a rewriting rule. The rewriting rule defines a regular expression of the "to be" URL and how it maps to the real url.
For example: I am using an imagehandler.ashx file to resize images on the fly. It takes query string parameters:
dir: the subdirectory of my pictures directory to use
file: the picture in that directory to use
width (optional): how wide to make the output picture
height (optional): how tall to make the output picture
This results in a url like this:
http://www.jessandrob.com/imagehandler.ashx?dir=Alberta-Aug-2007&file=DSC00105&width=800&height=600
Which clearly isn't to friendly.
After adding the reference I added this to the web config:
<urlrewritingnet xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
<rewrites></urlrewritingnet>
<add name="picture_web"
ignorecase="true"
virtualurl="^~/gallery/web/([\d\w\s\-]*)/([\d\w\s\-]*).aspx"
destinationurl="~/ImageHandler.ashx?Path=$1&File=$2.jpg&width=800&height=600"
rewriteurlparameter="ExcludeFromClientQueryString" />
</rewrites>>
After this the url becomes:
http://www.jessandrob.com/gallery/web/Alberta-Aug-2007/DSC00105.aspx
You can see my regular expression is matching digits, letters, whitespace, and dashes for both the directory name and the file name, and they are passed to the query string as "$1" and "$2" per standard Regex notation.
Notice its .aspx instead of .jpg - this seems to be a limitation of IIS. using ".JPG" actually works in the Visual Studio built in web server but fails in IIS - that's something I'll have to look into in the future.
Yes its that simple. I repeated the process for a couple other dynamic pages.
The one thing that did effect me a bit was URL's on the rewritten pages - you'll notice that the rewritten pages are deeper directory wise than the original - this is my design choice. This caused issues on the pages mapping relative urls - specifically I needed to utilize the ASP.NET "~" operator and have the url as runat=server (this was ASP.NET figures out the relative url for me). Where this isn't possible I have to manage the urls a bit more manually.
2 comments:
hello
I cannot add more then one rule in godaddy do you have any idea about that ?
these are the rules:
<add name="RewriteOnDomain" virtualUrl="^http\://(.*)/ilangoster/(.*)\.aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/ilangoster/default.aspx?id=$2" rewrite="Domain" ignoreCase="true"/>
<add name="SearchKategori" virtualUrl="^http\://(.*)/search/kategori/(.*)\.aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/search/default.aspx?kategori=$2" rewrite="Domain" ignoreCase="true"/>
"RewriteOnDomain" works but "SearchKategori" does not work. btw strange thing when I rename the "RewriteOnDomain" it does not work too. so stange
Hello Mascix,
Your regular expression is being greedy on you - you'll need to scale it back. Switching the order of these rules creates the opposite effect I'm guessing.
Regards,
Rob
Post a Comment