• home
  • forum
  • my
  • kt
  • download
  • URL Rewriting in ASP.NET using URLRewriter.Net

    Author: 2009-02-26 10:40:54 From:

    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.

    discuss this topic to forum

    relation tutorial

    No information

    Category

      NET (140)

    New

    Hot