What’s a Url Rewriting ?
Well, writing urls in such a friendly manner so that it can be more readable and seo-friendly, we use urlrewriting.
for example suppose i am having an url like
http://domainname.com/mypage.aspx?Id=1
after using url rewriting i can rewrite this url like
http://domainname.com/mypage.aspx/1
isn’t it more readable off course it is, and in this way search engines also recognizes your url better way and good page ranking can be achieved.
read in details regarding what’s url rewriting here
For now i am using urlrewritingnet a third party component to rewrite and redirect your urls.
so before proceeding ahead I want to also discuss regarding Url Redirection,
well url redirection is used to redirect an incoming http request from current url to any other url there are basically two types of redirections we used.
a) Http 301 (Permanent Redirect)
b) Http 302 (Temporary Redirect)
suppose some one requests for domanname.com/FirstPage and you want to display SecondPage in place of FirstPage so you can redirect user with any of the above technique.
read here regarding when to use which technique.
For Now let install this component to redirect the urls and also to redirect them.
a) Download the zip from here
b) Extract the zip and reference the urlrewritingnet.urlrewrite.dll to your project.
do the following steps inside web.config file
c) Arrange the config section inside configuration node following way
d) Create UrlRewritingnet Node this way
Your urls will be mapped here......
e) configure HttpHandler inside system.web which will listen to the requests
finally your whole code should look like this way
<?xml version="1.0"?>
Now url rewriting will work..
to give intellisense support include urlrewritingnet.xsd file into your project which is available in the package you downloaded.
here are some cool urls rewrite I done from this configuration
In the above image as shown in Image Code 100 if request made for Invoice/(.*) then request is handled by product.aspx page with query string parameter as category and this parameter we are received in code behind and the text is displayed.
In Image Code 101 if request made for Invoice/(.*)/(.*) then in two query string parameters are used one is category (as done before) another is price ,both the parameters are received in code behind.
In Image Code 102 if request made for Invoice/(.*)/(.*)/ then the query string parameters are the same way as done just before this in Image Code 101 and received them in code behind.
this is the code behind of Product.aspx page
protected void Page_Load(object sender, EventArgs e)
{
string item = Request.QueryString["Category"];
string price = Request.QueryString["Price"];
lblItem.Text = "You chosen the Category " + item;
if (price != null && price!="")
{
lblItem.Text += " and its price is " + price;
}
}
now what’s the interesting behind these all the mappings ???
The interesting thing which I noticed here is the prioritizing of mappings same as we have done in asp.net mvc routing so whenever a request is made for the request as virtual url the http handler checks the route which matches first and mapped to corresponding physical url,
so you should always put most-specific-mappings first in the way and then the less-specific mappings as in above we have put invoice(.*)/(.*)/ before invoice(.*)/(.*) because if someone requests for invoice/LG/20000/ and we’ve mapped the url first this one invoice(.*)/(.*) then at the last the price text will be 20000/ instead of being 20000,same way we put mapping invoice/(.*) after the most-specific mapping invoice/(.*)/(.*).