A Complete URL Rewriting Solution for ASP.NET 2.0

Ever wondered whether it's possible to create neater URLS, free of bulky Query String parameters? Gaidar Magdanurov shows you how with a step-by-step guide to his rewriting solution for ASP.NET 2.0.

This article describes a complete solution for URL rewriting in ASP.NET 2.0. The solution uses regular expressions to specify rewriting rules and resolves possible difficulties with postback from pages accessed via virtual URLs.

Why use URL rewriting?

The two main reasons to incorporate URL rewriting capabilities into your ASP.NET applications are usability and maintainability.

Usability

It is well-known that users of web applications prefer short, neat URLs to monstrous addresses packed with difficult to comprehend query string parameters. From time to time, being able to remember and type in a concise URL is less time-consuming than adding the page to a browser’s favorites to access later. Again, when access to a browser’s favorites is unavailable, it can be more convenient to type in the URL of a page on the browser address bar, without having to remember a few keywords and type them into a search engine in order to find the page.

Compare the following two addresses and decide which one you like more:

  1. http://www.somebloghost.com/Blogs/Posts.aspx?Year=2006&Month=12&Day=10
  2. http://www. somebloghost.com/Blogs/2006/12/10/

The first URL contains query string parameters to encode the date for which some blog engines should show available postings. The second URL contains this information in the address, giving the user a clear idea of what he or she is going to see. The second address also allows the user to hack the URL to see all postings available in December, simply by removing the text encoding the day ’10’: http://www.somehost.com/Blogs/2006/12/.

Maintainability

In large web applications, it is common for developers to move pages from one directory to another. Let us suppose that support information was initially available at http://www.somebloghost.com/Info/Copyright.aspx and http://www.somebloghost.com/Support/Contacts.aspx, but at a later date the developers moved the Copyright.aspx and Contacts.aspx pages to a new folder called Help. Users who have bookmarked the old URLs need to be redirected to the new location. This issue can be resolved by adding simple dummy pages containing calls to Response.Redirect(new location). However, what if there are hundreds of moved pages all over the application directory? The web project will soon contain too many useless pages that have the sole purpose of redirecting users to a new location.

Enter URL rewriting, which allows a developer to move pages between virtual directories just by editing a configuration file. In this way, the developer can separate the physical structure of the website from the logical structure available to users via URLs.

Native URL mapping in ASP.NET 2.0

ASP.NET 2.0 provides an out-of-the-box solution for mapping static URLs within a web application. It is possible to map old URLs to new ones in web.config without writing any lines of code. To use URL mapping, just create a new urlMappings section within the system.web section of your web.config file and add the required mappings (the path ~/ points to the root directory of the web application):

Thus, if a user types http://www.somebloghost.com/Support/Contacts.aspx, he can then see the page located at http://www.somebloghost.com/Help/Contacts.aspx, without even knowing the page had been moved.

This solution is fine if you have only two pages that have been moved to other locations, but it is completely unsuitable where there are dozens of re-located pages, or where a really neat URL needs to be created.

Another possible disadvantage of the native URL mapping technique is that if the page Contacts.aspx contains elements initiating postback to the server (which is most probable), then the user will be surprised that the URL http://www.somebloghost.com/Support/Contacts.aspx changes to http://www.somebloghost.com/Help/Contacts.aspx. This happens because the ASP.NET engine fills the action attribute of the form HTML tag with the actual path to a page. So the form renders like this:

Thus, URL mapping available in ASP.NET 2.0 is almost always useless. It would be much better to be able to specify a set of similar URLs in one mapping rule. The best solution is to use Regular Expressions (for overview see Wikipedia and for implementation in .NET see MSDN), but an ASP.NET 2.0 mapping does not support regular expressions. We therefore need to develop a different solution to built-in URL mapping.

The URL rewriting module

The best way to implement a URL rewriting solution is to create reusable and easily configurable modules, so the obvious decision is to create an HTTP Module (for details on HTTP Modules see MSDN Magazine) and implement it as an individual assembly. To make this assembly as easy to use as possible, we need to implement the ability to configure the rewrite engine and specify rules in a web.config file.

During the development process we need to be able to turn the rewriting module on or off (for example if you have a bug that is difficult to catch, and which may have been caused by incorrect rewriting rules). There should, therefore, be an option in the rewriting module configuration section in web.config to turn the module on or off. So, a sample configuration section within web.config can go like this:

This means that all requests that run like: http://localhost/Web/2006/12/10/ should be internally redirected to the page Posts.aspx with query string parameters.

Please note that web.config is a well-formed XML file, and it is prohibited to use the symbol & in attribute value strings. In this case, you should use & instead in the destination attribute of the rule element.

To use the rewriteModule section in the web.config file, you need to register a section name and a section handler for this section. To do this, add a configSections section to web.config:

This means you may use the following section below the configSections section:

Another thing we have to bear in mind during the development of the rewriting module is that it should be possible to use ‘virtual’ URLs with query string parameters, as shown in the following: http://www.somebloghost.com/2006/12/10/?Sort=Desc&SortBy=Date. Thus we have to develop a solution that can detect parameters passed via query string and also via virtual URL in our web application.

357-G_Magdanurov-feb07.gif

So, let’s start by building a new Class Library. We need to add a reference to the System.Web assembly, as we want this library to be used within an ASP.NET application and we also want to implement some web-specific functions at the same time. If we want our module to be able to read web.config, we need to add a reference to the System.Configuration assembly.

Handling the configuration section

To be able to read the configuration settings specified in web.config, we have to create a class that implements the IConfigurationSectionHandler interface (see MSDN for details). This can be seen below:

The Class RewriteModuleSectionHandler will be initialized by calling the Create method with the rewriteModule section of web.config passed as XmlNode. The SelectSingleNode method of the XmlNode class is used to return values for module settings.

Using parameters from rewritten URL

When handling virtual URLS such as http://www. somebloghost.com/Blogs/gaidar/?Sort=Asc (that is, a virtual URL with query string parameters), it is important that you clearly distinguish parameters that were passed via a query string from parameters that were passed as virtual directories. Using the rewriting rules specified below:

You can use the following URL:

http://www. somebloghost.com/gaidar/?Folder=Blogs

…and the result will be the same as if you used this URL:

http://www. somebloghost.com/Blogs/gaidar/

To resolve this issue, we have to create some kind of wrapper for ‘virtual path parameters’. This could be a collection with a static method to access the current parameters set:

You can see from the above that it is possible to access ‘virtual path parameters’ via the RewriteContext.Current collection and be sure that those parameters were specified in the URL as virtual directories or pages names, and not as query string parameters.

Rewriting URLs

Now let’s try some rewriting. First, we need to read rewriting rules from the web.config file. Secondly, we need to check the actual URL against the rules and, if necessary, do some rewriting so that the appropriate page is executed.

We create an HttpModule:

When adding the RewriteModule_BeginRequest method that will process the rules against the given URL, we need to check if the given URL has query string parameters and call HttpContext.Current.RewritePath to give control over to the appropriate ASP.NET page.

We must then register this method:

But this is just half of the road we need to go down, because the rewriting module should handle a web form’s postbacks and populate a collection of ‘virtual path parameters’. In the given code you will not find a part that does this task. Let’s put ‘virtual path parameters’ aside for a moment. The main thing here is to handle postbacks correctly.

If we run the code above and look through the HTML source of the ASP.NET page for an action attribute of the form tag, we find that even a virtual URL action attribute contains a path to an actual ASP.NET page. For example, if we are using the page ~/Posts.aspx to handle requests like:

http://www. somebloghost.com/Blogs/2006/12/10/Default.aspx,

…we find the action=”/Posts.aspx”. This means that the user will be using not the virtual URL on postback, but the actual one: http://www. somebloghost.com/Blog.aspx. This is not what we want to use here! So, a few more lines of code are required to achieve the desired result.

First, we must register and implement one more method in our HttpModule:

This method checks if the user requested a normal ASP.NET page and adds a handler for the PreInit event of the page lifecycle. This is where RewriteContext will be populated with actual parameters and a second URL rewriting will be performed. The second rewriting is necessary to make ASP.NET believe it wants to use a virtual path in the action attribute of an HTML form.

Finally, we see three classes in our RewriteModule assembly:

357-G_Magdanurov%282%29-feb07%20%282%29.

Registering RewriteModule in web.config

To use RewriteModule in a web application, you should add a reference to the rewrite module assembly and register HttpModule in the web application web.config file. To register HttpModule, open the web.config file and add the following code into the system.web section:

Using RewriteModule

There are a few things you should bear in mind when using RewriteModule:

  • It is impossible to use special characters in a well-formed XML document which is web.config in its nature. You should therefore use HTML-encoded symbols instead. For example, use & instead of &.
  • To use relative paths in your ASPX pages, you should call the ResolveUrl method inside HTML tags: <img src=”<%=ResolveUrl(“~/Images/Test.jpg”)%>” />. Note, that ~/ points to the root directory of a web application.
  • Bear in mind the greediness of regular expressions and put rewriting rules to web.config in order of their greediness, for instance

  • If you would like to use RewriteModule with pages other than .aspx, you should configure IIS to map requests to pages with the desired extensions to ASP.NET runtime as described in the next section.

IIS Configuration: using RewriteModule with extensions other than .aspx

To use a rewriting module with extensions other than .aspx (for example, .html or .xml), you must configure IIS so that these file extensions are mapped to the ASP.NET engine (ASP.NET ISAPI extension). Note that to do so, you have to be logged in as an Administrator.

Open the IIS Administration console and select a virtual directory website for which you want to configure mappings.

Windows XP (IIS 5)
Virtual Directory “RW”

357-G_Magdanurov%283%29-feb07.gif

Windows 2003 Server (IIS 6)

Default Web Site

357-G_Magdanurov%284%29-feb07.gif

Then click the Configuration… button on the Virtual Directory tab (or the Home Directory tab if you are configuring mappings for the website).

Windows XP (IIS 5)

357-G_Magdanurov%285%29-feb07.gif

Windows 2003 Server (IIS 6)

357-G_Magdanurov%286%29-feb07.gif

Next, click on the Add button and type in an extension. You also need to specify a path to an ASP.NET ISAPI Extension. Don’t forget to uncheck the option Check that file exists.

357-G_Magdanurov%287%29-feb07.gif

If you would like to map all extensions to ASP.NET, then for IIS 5 on Windows XP you have only to map .* extension to the ASP.NET ISAPI extension. But for IIS 6 on Windows 2003 you have to do it in a slightly different way: click on the Insert… button instead of the Add… button, and specify a path to the ASP.NET ISAPI extension.

357-G_Magdanurov%288%29-feb07.gif

Conclusions

Now we have built a simple but very powerful rewriting module for ASP.NET that supports regular expressions-based URLs and page postbacks. This solution is easily implemented and gives users the ability to use short, neat URLs free of bulky Query String parameters. To start using the module, you simply have to add a reference to the RewriteModule assembly in your web application and add a few lines of code to the web.config file, whereupon you have all the power of regular expressions at your disposal to override URLs. The rewrite module is easily maintainable, because to change any ‘virtual’ URL you only need to edit the web.config file. If you need to test your application without the module, you can turn it off in web.config without modifying any code.

To gain a deeper insight into the rewriting module, take a look through the source code and example attached to this article. I believe you’ll find using the rewriting module a far more pleasant experience, than using the native URL mapping in ASP.NET 2.0.

For more articles like this, sign up to the fortnightly Simple-Talk newsletter.

Downloads

Tags: , , , , , , , , , , ,

  • 558824 views

  • Rate
    [Total: 1551    Average: 2.2/5]
  • Anonymous

    The definitive resource for ASP.Net 2.0 URL rewrites
    There are many articles that pretend to address this topic, but this is the first complete explanation that I have seen. Thank you.

  • Anonymous

    Extension-less URL Rewriting
    While your article is good, you neglect to mention how to accomplish Extension-less URL Rewriting
    despite the fact you mention URLs without extensions quite a bit:
    e.g. http://www. somebloghost.com/Blogs/2006/12/10/

    Scott Guthrie covers this (and a lot of the same ground you cover) in his article over at

    http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

  • Anonymous

    Complete one
    I have gone through many article on same topic, this is one where i found all most all the subtopic what i needed
    thanks

  • Anonymous

    Good article
    But there is one problem:

    How is somebody going to edit IIS properties on shared hosting server?

    IIS sucks, period.

  • Anonymous

    Good Article
    Yes , it Helped me a Lot

    Thank you Very Much

  • Anonymous

    Good Article
    Yes, its really a good article.

    Thanks for this

  • Anonymous

    Good article
    thanks

  • Anonymous

    Rule Question
    Hello. What would be the proper way to set up a rule in Web.config to point:

    /Test/1.aspx

    to

    /Test.aspx?id=1

    I tried the following, but it didn’t seem to work:

    <rule
    source=”~/Test/(.*).aspx”
    destination=”~/Test.aspx?Source=$1″ />

    Also tried:

    <rule
    source=”~/Test/(d{1,5}).aspx”
    destination=”~/Test.aspx?Source=$1″ />

    Any advice?

    Thanks!

  • Anonymous

    urgent (need Help)
    I have done this part in web.config file and it is working when i start from local machine.
    like ==> http://localhost:4683/Rentslicer/California

    But when i was try to start from my ip address it is not working .
    like ==>http://125.6.3.139/California

    So what should i do??

    <urlMappings enabled=”true”>
    <add url=”~/California” mappedUrl=”~/state.aspx?State=CA&alt=California”/>
    <add url=”~/Washington” mappedUrl=”~/state.aspx?State=WA&alt=Washington”/>

  • Anonymous

    IIS6 404?
    Great article!

    I have followed your setup etc. and no matter what I try, IIS6 (Win2k3 server) returns a 404 when I try to access the URL that needs to be re-written. For example:

    http://www.foo.com/Books/123 should be re-written as http://www.foo.com/Books.aspx?ID=123

    but IIS always seems to return the generic 404 error page (“page or directory not found” – there is no “Books” directory on the web root). Any thoughts to what IIS setting might be causing this?

    Thanks!

  • Anonymous

    Re : IIS6 404 ( By Bhaskar Bahal)
    Hi i have temporary Solution for this if you put
    ttp://www.foo.com/Books/123.aspx Then it will work.

  • Anonymous

    Re: IIS6 404?
    I tried that (adding file extention to end)….as noted in some of the earlier comments.

    Any other thoughts (IIS configuration settings, etc.)?

  • Anonymous

    Answers
    Hello everybody!

    Thank you for your interest. I will try to answer all of your questions:

    1. If you want to rewrite /Test/1.aspx to /Test.aspx?id=1 you should use the following rule:

    <rule source=”Test/(.*)Default.aspx” destination=”/Test.aspx?id=$1″/>,

    2. Question with IP address is not for my rewriting solution but for integrated URL mapping functionality in ASP.NET. But I can answer – you have to configure IIS web server to handle .* (all extensions) with ASP.NET ISAPI filter to use extensionless URLs

    3. As for 404 error – double check if you turn off Verify That File Exist option in IIS configuration for .* “extension”

    Best,
    Gaidar

  • Anonymous

    Intresting
    This article is really Helpfull

  • Anonymous

    url rewriting
    Hi every one explaining the Url Rewriting its really very usefully.can u explain very clearly and deeply

  • Anonymous

    Thanks Gaider
    Hi I am using Your Code in my application.
    I have two problem.

    1. On Production server the images of mapped url page is not showing.

    2. Css is not apply to Pages.

    Please correct me.

  • Anonymous

    Gaider
    Thanks Gaider Youhelp me lot

    Now I am using Above code.But there is two problem.
    1. I have to use every where Resolve url in my project.

    <a class=”BlueLink” href= “<%=ResolveUrl(“ViewAllMarket.aspx?state=” + state )%>”>View all markets ››</a>
    instead of
    <a class=”BlueLink” href=”ViewAllMarket.aspx?state=<%=state%>”>View all markets ››</a>

    Is there any thing So that i can use directly Href tag instead of Resolve url.
    Because in project i have to change all URl and Image path
    PLEASE suggest me its urgent

  • Anonymous

    Problem
    I use code in my web site, but after set in IIS configuration for .* “extension” checkbox – “check that file exist” on off.

    1. CSS style is not apply to pages

    2. When try to open file from virtual folder

    System.Drawing.Image.FromFile(Server.MapPath(“~/” + ConfigurationManager.AppSettings[“Images”]) + imageURL);

    i catch exception ot type “FileNotFoundException” , but file exist and path is correct.

    if i set checkbox on “on” then these problem not exist, but urlrewrite on type:

    from “mysite/product/1234”
    to “mysite/product.aspx?id=1234” don’t work.

  • Anonymous

    Great
    Hi.. I found many articles on this..its nice to see much more explanation on this article..

    Great work…

  • Anonymous

    hide aspx to htm
    plz sugget some way to hide aspx as htm

  • Anonymous

    Not invented here?
    You could just try using an open source url rewriter like http://urlrewriter.net/

  • Anonymous

    Help
    Gaidar Magdanurov,

    Thanks for your post. We have used your code in our project but we are facing a problem that its working fine in 5.1 but not working in 6.0. Please give the solution for this.

    You can check this problem online at:
    http://www.bramptonpages.com/8/Electronics-And-Semiconductors/Default.aspx

    But the same is working on:
    http://localhost/BramptonPages/8/Electronics-And-Semiconductors/Default.aspx

    Thanks in advance

  • Anonymous

    Why not just use Global.asax?
    For those of you that are on shared hosting try using the following code in this article:

    http://www.willasrari.com/blog/rewrite-url-without-using-filenames-or-extensions-c/000109.aspx

  • Anonymous

    rewrite not working on live server
    I was able to get your code to work on my local development machine but when I put it on the live server and checked what was being compared, RewriteBase had an extra slash at the beginning i.e. //blogs/default.aspx but the path only had 1 slash. I found this out by raising an error if there was no match and displaying the 2.
    If I try to do a replace the page that keeps trying to load.
    I am not using and Regular expression comaparisons just blogs/derectory/default.aspx for the source and blogs/default.aspx for the destination.

    Thanks
    Royal

  • Anonymous

    rewrite not working on live server
    I was able to get your code to work on my local development machine but when I put it on the live server and checked what was being compared, RewriteBase had an extra slash at the beginning i.e. //blogs/default.aspx but the path only had 1 slash. I found this out by raising an error if there was no match and displaying the 2.
    If I try to do a replace the page that keeps trying to load.
    I am not using and Regular expression comaparisons just blogs/derectory/default.aspx for the source and blogs/default.aspx for the destination.

    Thanks
    Royal

  • doggy8088

    There is a bug !! Please see here.
    Your module will not execute smoothly when install website in root web application:

    Line 36 of RewriteModuleSectionHandler.cs:

    _RewriteBase = HttpContext.Current.Request.ApplicationPath + “/”;

    Should rewrite as

    if (!HttpContext.Current.Request.ApplicationPath.EndsWith(“/”))
    {
    _RewriteBase = HttpContext.Current.Request.ApplicationPath + “/”;
    }

    That’s because when your web ap run in root url, the HttpContext.Current.Request.ApplicationPath will be “/”, if you append an “/” will cause file not found error.

  • Anonymous

    Avoid .* and make sure you’re not missing files
    Mapping .* to aspnet_isapi means that every file on the web server is served up by ASP.NET. You’re giving .NET processing duties that are normally handled by IIS. When a page loads, .NET isn’t handling one page, it’s handling one page PLUS any number of .css, .js, .gif, .jpg, or .png files. Multiply that by every page load of every user of every site on the server, and .NET’s performance starts to degrade.

    Instead, you can set the custom 404 error in IIS to point to something like URL /404.aspx and handle the rewriting from there. That way, .NET only needs to handle missing files, like “/MyVirtualPage/”.

    In either case, it’s important to make sure you aren’t missing any images or linked files. If any file is missing, ASP.NET will return its 404 error page (or your custom 404 page), doing extra work for no reason.

    Now let’s take it a step further to witness the destructive potential of a single missing image. Let’s say you have a master page (or a control used on all pages), and it has an image tag pointing to an image that no longer exists. When it can’t find the image, the Web.config tells .NET to load your custom 404 page. But your custom 404 page uses the same master page with the same missing image, so now you’ve got an infinite loop, dragging down the performance of the entire server.

  • Anonymous

    Scott Mitchell…
    It appears as if you based this off Scott Mitchell’s URL Rewriting guide. You should definitely cite your sources….

  • Anonymous

    Virtual url files
    My assumption is that the virtual url doesn’t have to really exist but if I use the below rule I get the error
    The resource cannot be found.
    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /Rewrite.Test/blogs/Caroline/blogs.aspx

    Rule:
    <rule source=”(d+)/(d+)/(d+)/” destination=”Default2.aspx?blogID=333″/>

    I didn’t think that the caroline/blogs.aspx had to really exist.

    Thanks
    Royal

  • Anonymous

    Maintainability and Usability
    A good article, although usability and maintainability are not good reasons to consider rewrting your urls. A user experienced enough to manipulate the flattened will be able to manipulate the dynamic version. I also do not want users manipulating url’s if i can help it.

    However rewriting your urls as meaningful textual phrases is a great idea, not just for the user but more importantly for search engines!!!

  • Anonymous

    Answers

    Sorry that it took too long for me to get here and answer your questions – I do apologize but I’m really busy these days.

    First of all, most of the problems arise because of relative paths, so it is better to use paths starting from the root. Thus, you should use the following for an image:
    <img src=”<%=ResolveUrl(“~/Images/MyImage.jpg”)%>” />
    or the following for CSS:
    <link rel=”stylesheet” href=”<%=ResolveUrl(“~/Css/MyCss.css”)%>” type=”text/css” />

    Of course, you may use direct href tag if you are specifying url starting from the root, e.g.:
    <a href=”/MySite/Page/MyPage.aspx” />

    This is a price you have to pay for URL rewriting…

    The next “problem generator” is regular expression you are using…

    For BramptonPages – could you please show me rewriting rules you are using?

    Doubled slash (//) not raising issues actually because it is simple ignored… But your solution is good, I had to do that. Thanks!

    As for mapping .* to ASP.NET – I myself don’t like the idea (maybe because I’m served by ASP.NET I like to know it is ASP.NET from the URL and not .php or .pl :)).

    As for Scott Mitchell’s article. I have not seen it before because I have been using this type of rewriting for more than 3 years now and never Google/Live/Yahoo and etc. for rewriting solutions…

    As for this rule:

    <rule source=”(d+)/(d+)/(d+)/” destination=”Default2.aspx?blogID=333″/>

    Your regular expression simply is wrong – it will do for something like /Rewrite.Test/10/20/30/. If you like to use a rule for /Rewrite.Test/blogs/Caroline/blogs.aspx then you have to use the following rule:

    <rule source=”(w+)/(w+)/(w+).aspx/” destination=”Default2.aspx?blogID=333″/>

    Thank you all for your comments!

  • subrat

    Urgent
    Hi I am using Your Code in my application.
    I have two problem.

    1. On Production server the images of mapped url page is not showing.

    2. Css is not apply to Pages.

    Please correct me.

  • Anonymous

    urgent reply me plz
    When i go for live server it is giving 404 error
    plz tell be very urgent
    <rewriteRules>
    <rule source=”(.+)/(.+)/(.+)/” destination=”Neighbourhood.aspx?StateName=$1&CityArea=$2&neigh=$3″/>
    </rewriteRules>

    Please tell me is there i have to config iis
    My url like
    http://www.mycompany.com/california/losangles/mailbu/
    is not working

  • Florin L.

    Alternative
    I just tried “urlrewriting.net” and it does the trick, theme and image urls been handled properly.

    Binary, Docs and Sources at:
    http://www.urlrewriting.net/

  • subrat

    Query
    When i mapped the URL my java script function is not working . How can it works . please mail me.

  • Nauman Ahmed

    Nice effort
    Very nice article

  • subrat

    Query
    When i mapped the URL my java script function is not working . How can it works . please mail me.

  • Anonymous

    Seems like it just doesn’t trigger on the live site
    This works fine when developing on my local machine:

    <rule source=”(w+)/test.aspx” destination=”$1.aspx”/>

    but once pre-compiled and loaded on our development server, I get the error:

    Server Error in ‘/’ Application.
    The resource cannot be found.
    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /search/test.aspx

    Any clue what I’m missing?

  • Vipul Parekh

    Can I get the sample code in vbscript
    Hi,

    This is the most detailed article I found on net. It will be gr8 if you can provide it in vbscript.

    Regards,
    Vipul Parekh

  • Choong

    run RewriteModule in Medium Trust
    Thank you for the fantastic sharing. This is really a good site to know how to rewrite the URl.

    However, when it is compile to DLL, it will not run in shared hosting running under Medium Trust.

    How and what should we do to counter this problem? Thank you.

  • Meghana

    Not working online
    This example is not working online. Please help for the same

  • candyman

    Not working in share hosting.
    I tested it in local, and it runs well.But I upload DLL in a share hosting, it can not run, please tell me why?. http://vnexhibition.net/ =>this’s my site.

  • Anonymous

    Urgent:: NOT GETTING ACCESS
    Your code is in detail and specific but the problem i faced is :
    my default URL is “http://localhost:1555/XYZ/mn.aspx”
    and when i use the “http://192.168.1.2/XYZ/mn.aspx” ,which is the IP of the same machine, i could not access the pages, wherever the prior URL works properly.
    For this reason i am not getting access these pages from other machines on the LAN and not from the internet (my server is configured as WEB server).
    Plz solve my problem, i am not getting any solution. I had seen this type of problem in this topic but did not get any solution from you. So, plz solve my problem as early as possible.

    thanks.

  • Anonymous

    to fetch url of the address bar in a string
    hello sir,

    how to fetch url(link) of the address bar (web page) in a string????????????[asp.net2.0 with c#)

  • Anonymous

    re:to fetch url of the address bar in a string
    try
    HttpContext.Current.Request.RawUrl

  • Bhaskar Bahal

    Re:Urgent:: NOT GETTING ACCESS
    Hey This is for all
    Please configure IIS other wise this will not work.
    To handle .* (all extensions) with ASP.NET ISAPI filter to use extensionless URLs like *.aspx

    Then it will work
    http://192.168.1.2/XYZ/mn.aspx

  • Viraj

    Gud Solution
    i m new to url rewriting.
    here i found very easy solution to start with.
    i appriciate the efforts…..

  • Anonymous

    Url Rewriting/Mapping
    Hello, Can u give me this code in vb.net insted of using c# language. ?

  • Sem GÖKSU = askisem@hotmail.com

    Not working online
    This example is not working on Windows Server 2003(iis 6.0). Please help for the same…

    Sem GÖKSU = askisem@hotmail.com

  • Viru

    Urgent !!! HttpContext.Current.Request.Path is working strangely
    Hi,

    I have tried your code in 2 applications.
    One is working fine. The other is not and so I have describing that here.

    Suppose,
    There is one page
    /project/productslisting.aspx?cat=3
    I rewrite it as /project/cat/3/productslisting.aspx
    It is working correctly. Now, the HttpContext.Current.Request.Path is /project/cat/3/productslisting.aspx

    On this page paging has been applied, so as soon as I click on next page(i.e. LinkButton)
    HttpContext.Current.Request.Path is changed to /project/productslisting.aspx

    How strange! Please help me out because it works in other application perfectly. Thanks!

  • Viru

    Urgent !!! HttpContext.Current.Request.Path is working
    Hi,
    I have found the bug. I have used <base href=”http://project/”> tag to overcome the problem of css,images rendering so URL rewriting is not working and Request.path is changed. If I remove <base> css and images will not render and path will be proper. Now what should I do?

  • Yearby
  • Anonymous

    how to write a rule.
    hi,
    i have face one problem in that i have to need genrealized create a rule for the ex.

    http://localhost/News.aspx
    with
    http://localhost/News
    with same as any page’s coming like
    http://localhost/(pageName) and destination is that
    (pagename).aspx

  • Anonymous

    how to write a rule.
    hi,
    i have face one problem in that i have to need genrealized create a rule for the ex.

    http://localhost/News.aspx
    with
    http://localhost/News
    with same as any page’s coming like
    http://localhost/(pageName) and destination is that
    (pagename).aspx

  • Anonymous

    whatever
    What a waste of time… Who cares if your URL looks nice or not.

  • hardik joshi

    url rewrite error
    When i use the above module in my application it gives me the error :–“The system cannot find the path specified.”

    I really need help in this. have i made any mistake in configuration.
    please reply me tech2hard@gmail.com

  • Simon

    Brilliant
    This is a fantastic solution. You don’t know how hard I’ve tried to get other URL rewriting solutions working, and this one works perfectly for me. Thakns a lot!

  • Ambuj

    Works in filesystem based website but not in IIS website
    Hello, excellent article!
    I’ve been trying it out but due to some reason if I create a file system based website in VS2005, the redirection works. But when I create the website in IIS and run it from VS2005, it doesn’t break into RewriteModule_BeginRequest and hence doesn’t redirect – Any ideas? Thanks in advance

  • Anonymous

    Redirecting from aspx to other.
    Hello. Thanks for your article its very good. I have functionating the rewriting. Thats meen when somebody do http://www.site.com/help its going to the good place http://www.site.com/newsite/helpContext/help.aspx (something like this)
    But after that if a user go to a menu link it go to: producto.aspx?idP=1789& … and I wold like that at that moment the URL that see the user was transformed to http://www.site.com/products

    It is possible? Thank you very much
    Natalia

  • Hi need Help. Can any one help..

    URL Rewriting Help
    Hi all.. Im struggling to implement URL Rewriting. Can anyone please help me.

    I have list of category and sub-category.

    Eg., Category – Cars
    Subcategory – Ford,Hyundai,etc..

    When i select Cars, Im getting, Category.aspx?Catid=1
    I dont want this kind of query string.
    I want like user friendly query string like Category/cars/1/ or like tat.

    How to implement this. I mean How to call the begin request to inline code or codebehind where we bind data to control..like

    ==================================
    default.aspx.cs (code behind)

    GridView1.Datasource = ds;
    GridView1.Databind
    ==================================
    default.aspx (Inline code or source)
    ==================================

    <asp:GridView ID=”GridView1″ runat=”server”>
    <Columns><asp:TemplateField>
    <ItemTemplate>
    <a href=“category.aspx?catid=“<%#Eval(“CatID“)%>
    “><%#Eval(“CatName“)%></a>
    </ItemTemplate>
    </asp:TemplateField></Columns>
    </asp:GridView>

    How to implement the URL rewrite here. Please help me to get rid out of this. Thanks in advance…

  • rueny01

    Plss help
    Could not load file or assembly ‘RewriteModule’ or one of its dependencies. The system cannot find the file specified. (<drive name>:Documents and Settings<user name><folder name>UrlReWritingweb.config line 34)

    Thanks in advance

  • parani

    Error in Web.config
    Could not load file or assembly ‘RewriteModule’ or one of its dependencies. The system cannot find the file specified.

  • Henryzhchen

    Error when publishing RewriteModule
    I open the “RewriteModule” with VS 2005 on Windows Vista. It’s fine without publishing it. When I publishing it to another site, I got
    “HTTP Error 500.0 – Internal Server Error”

    I run the “%systemroot%system32inetsrvAPPCMD.EXE migrate config “Default Web Site/TestRewriteModule” from command line as recommended, I got following another error:

    Parser Error Message: An error occurred creating the configuration section handler for modulesSection/rewriteModule: Error while processing RewriteModule configuration section.

    Source Error:

    Line 15: </configSections>
    Line 16: <modulesSection>
    Line 17: <rewriteModule>
    Line 18: <rewriteOn></rewriteOn>
    Line 19: <rewriteRules>

    Can anyone help on this.

    Thanks in advance.

  • Sarah Grady

    Anonymous Comments Disabled
    Due to a high volume of spam, anonymous comments have been disabled.

  • prashantvictory

    Rule Question
    I want to that if user type the following url like
    mywebsite/blog
    then the destination url should be
    mywebsite.com/blog?which=blogstrip
    How can i do this?????

  • krimashah

    PROBLEM Urgent
    i have used the same DLL file , and i hava add that referrences in BIN. Now , in Web.config if i m write <modulesSection> then it gives me error , like Unorganized modulesSection in web.config.Pls help me.

  • krimashah

    PLS help me
    it is not workin on thw live , like i want http://www.roadtriphelper.com/locate/pune/ then it will display a map of pune , but is it not working on live server. Pls help me.

  • neerajmude1979

    Site working slow
    Dear Sir,
    First of thanks for this ultimate program. Here i m using this tool for my website and the site is online, But it is working too much slow. Rather than other sites are working too fast and i found the cause is web.config Here i writes all rule for rewriting in web.config and it become too slow.

    So please give me suggestion to improve the speed for site and also the main problem of image path. i m included basic path with http:// . Just i m in confusion so please help me ASAP.

    Thanks
    Neeraj Mude
    S/W Eng.

    neerajmude1979@gmail.com

  • neerajmude1979

    Please Reply Soon
    Respected Sir ,

    Please reply soon. I m waiting …….

    neerajmude1979@gmail.com

  • Kimsea

    Error Hosting
    Dear Gaidar Magdanurov,

    Your code work well, when I run with Microsoft Visual Studio Professional 2005 on my local machine. Then I run this code on my Hosting, it didn’t work. And I ask my hosting he reply that your dll url rewrite did not work.

    Please help me,
    Thank You,

  • Kimsea

    Please Help Me
    Dear Gaidar Magdanurov,

    Please help me for your code. Because I use this code make URL Rewriting, But it work only run with Visual Studio Professtional 2005. And I publish this code and put on my hosting doesn’t work.

    How should I do?

    Thank you,

  • neilx

    SiteMapDataSource
    This works for me apart from using a SiteMap with a SiteMapDataSource and a Menu Control. No virtual folder is appended.

    Any ideas on how to do this?

  • http://www.aspdotnetfaq.com

    It works!
    Works perfect!

    Thanks,
    Slobo

  • http://www.aspdotnetfaq.com

    It works!
    Works perfect!

    Thanks,
    Slobo

  • anildesai

    Problem when i will use Two pages.
    Here i have to pages first is Post.aspx and second is NewPost.aspx.

    I write this rules for both pages.

    <rewriteRules>
    <rule source=”(w+)/(w+)/” destination=”NewPost.aspx?Name=$1&Age=$2″/>
    <rule source=”(d+)/(d+)/(d+)/” destination=”Post.aspx?Year=$1&Month=$2&Day=$3″/>
    <rule source=”(.+)/Default.aspx” destination=”Default.aspx?Folder=$1″/>
    </rewriteRules>

    Here i m calling Post.aspx page by..
    <li><a href=”<% = ResolveUrl(“~/2000/01/01/”) %>”>Date in the URL 1</a></li>

    and calling NewPost.aspx page by..
    <li><a href=”<% = ResolveUrl(“~/sdfsdf/kkkkkkk/”) %>”>Student Information</a></li>

    Here problem is only NewPost.aspx page is call for both above link..
    I want to call Post.aspx …

    thanks.

  • anildesai

    Problem when i will use Two pages.
    Here i have to pages first is Post.aspx and second is NewPost.aspx.

    I write this rules for both pages.

    <rewriteRules>
    <rule source=”(w+)/(w+)/” destination=”NewPost.aspx?Name=$1&Age=$2″/>
    <rule source=”(d+)/(d+)/(d+)/” destination=”Post.aspx?Year=$1&Month=$2&Day=$3″/>
    <rule source=”(.+)/Default.aspx” destination=”Default.aspx?Folder=$1″/>
    </rewriteRules>

    Here i m calling Post.aspx page by..
    <li><a href=”<% = ResolveUrl(“~/2000/01/01/”) %>”>Date in the URL 1</a></li>

    and calling NewPost.aspx page by..
    <li><a href=”<% = ResolveUrl(“~/sdfsdf/kkkkkkk/”) %>”>Student Information</a></li>

    Here problem is only NewPost.aspx page is call for both above link..
    I want to call Post.aspx …

    thanks.

  • hellosibba

    How to access the query string parameters
    How do i fetch the values of query string.. say i put the id in name value pair.
    Actual URL : ~/Tag.aspx?id=5
    Displayed Virtual URL :~/tag/5.html
    I have used rule to map ~/tag/5.html to ~/Tag.aspx?id=5
    and the page is opening.

    The virtual URL is mapped to actual page.
    I got the issue where hot to get the Id=5.
    Do i need to parse the entire URL, which can be devastating. Neither i am getting in path info.
    Is there any place where i’ll get the parameters in some name value collection….

    Information Requested!!!!
    Thanks in advance.

  • saumya

    throws a 404 exception
    The article is great
    I tried the same code and it works on ASP.net development server but it does not work on IIS

    Ex it works on http://locahost:18345/web/default.aspx
    but it does not work on
    http://localhost/default.aspx

    Please advice
    Thanks

  • saumya

    throws a 404 exception
    http://localhost/ThisIsATest/Default.aspx
    cannot be found server/application error , i have unchecked the check file exists box

  • raj_v15

    Please Help Me
    Hi,

    I have a link like the one below:

    http://localhost:2243/magicmoments/ViewAlbum.aspx?aid=1909087d-69c3-4970-909d-31d5e9fb9543

    now i want to display it as

    http://localhost:2243/magicmoments/1909087d-69c3-4970-909d-31d5e9fb9543

    can you help me what the rule should be for this.
    Thanks in advance.

  • jabailo

    Deployment
    How do I deploy this application to another machine?

    Assuming that I do not compile this on the target machine:

    Into what directory do I copy the .dll?

    Do I add to the global assembly cache? Or run regsvr32?

    How do I get to the point of being able to register the module in web.config?

  • jabailo

    Deployment
    How do I deploy this application to another machine?

    Assuming that I do not compile this on the target machine:

    Into what directory do I copy the .dll?

    Do I add to the global assembly cache? Or run regsvr32?

    How do I get to the point of being able to register the module in web.config?

  • sudheshna

    Windows server 2003

    The example given is not working on windows server 2003. Please let me know if there is any solution for this.

    Thanks

  • jabailo

    Cannot Run as Website, only Virtual Directory
    I was able to publish the Release code to a virtual directory on a remote server. After setting the wildcard filter to aspnet_isapi.dll the same website worked fine.

    However, if I try to run it as it’s own website, with its own IP address and do exactly the same thing, I get the 404 error!

    In fact I made a virtual directory in the website, and pointed it to the same code, then made the isapi change, and it worked as well!

    But I cannot run it as the top level of the website.

    Can you suggest why this might happen?

  • SandeepKumar

    This solution is NOT recommended. Use urlrewriter.net
    This thing is not working in IIS 6 Win2K3 website.
    Me and my team wasted lots of time using this, and found this UrlRewriter.NET.

    UrlRewriter.NET is an open source solution which works with Win2K, Win2K3 and WinXP; and compatible with ASP.NET 1.1 and 2.

    It has good regular expression support and lots of other features.

    Please have a look, http://urlrewriter.net/index.php/support

    For ASP.NET 3.5 check this article by scottgu http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

  • jabailo

    Works good…only as vdir
    This solution works perfectly — but only as a vdir. I’m suspecting there’s a bug in configuring the root (website) level wildcard mappings in IIS 6.0.

    I am hoping someone will respond to my challenge on this…

  • NghiaOi

    can you help me
    i uesd asp.net 2005 rewrite url

    runing: http://localhost:1458/Milk/About/

    don’t runing: http://localhost/Milk/About/

  • abhishek1985

    Postback problem
    If I am trying to postback Post.aspx page at that time URL in my Address bar is http://localhost:2748/Web/2001/12/24/

    Than its thrown error like below

    The HTTP verb POST used to access path ‘/Web/2001/12/24’ is not allowed.

    Can you please help me in this

  • ankush_lee

    Url Rewriting
    Hi,

    The code seems to be perfect, i have tried the code in my application & it seems to working properly. All what i want to know is how to redirect to the page for which i have set rules in web.config… I am unable to get the way for that. Can any one help for the same.

    Thanks in advance

  • vaib

    postback problem
    Hi,
    Thanks for write this code.This is perfect code for url rewriting.I try this.This work perfectly and rewrite url properly.But this create problem if I click on any button,This is not understand postback and on every page postback it generate new request,result of this my button click event not fired.
    If any one know solution of this.Please help me .This is very urjent.

    Thanks

  • niico

    only works with built in visual studio web server – not IIS
    hi

    great article thanks.

    I can get it to work from within the visual studio web server – but in IIS only the simple directory example exists.

    Ive added .* to mapping – but still nothing.

    Thanks!

  • niico

    update
    hi

    further to my post above:

    i also cant get either example to work on the webserver (windows server 2003R2) – ive followed the IIS 6 instructions to let the aspnet_isapi.dll intercept everything with no file extension.

    please help?

  • denniswong288

    Google indexing
    Hi,

    I’m using urlrewriting.net for my site’s URLRewriting.

    Was wondering if Google indexes pages using urlrewriting.net? or is this all a hype?

    The thing is I’ve seen numerous posts that Google does not index pages using “urlrewriter.net”.
    However, I’ve not seen any regarding “urlrewriting.net”.

    Please note that these are 2 separate UrlRewriting solutions from 2 different teams/companies.

    Thanks!

  • DotNetGuts

    URL Rewriting Example
    Your article is good, but I have describe some simple and easiest way to perform URL Rewriting

    http://dotnetguts.blogspot.com/2008/07/url-rewriting-with-urlrewriternet.html

  • senthil

    Native Url Mapping
    Hi,
    Hope you are fine. I am very strange to ASP.NET. I have a query that how to implement URL rewriting, when my project hosted in Local IIS server?. Because i had done Native URL mapping as you said. But it succeeded only when i am working local connection ( I meant when the file exist in System drive ). When i want to implement the same in IIS server, it doesn’t work. What is the solution?

  • senthil

    Native Url Mapping
    The error says “The page cannot be found”

  • valmont33

    IIS6 Root Fix
    If you are experiencing generic 404’s when running your site from the root (http://localhost/page.aspx) or on a live shared server please make the following change to the original code.

    File: RewriteModuleSectionHandler.cs
    Line: 36
    Orinial:
    RewriteBase = HttpContext.Current.Request.ApplicationPath + “/”;

    Change To:
    if (!HttpContext.Current.Request.ApplicationPath.EndsWith(“/”))
    {
    _RewriteBase = HttpContext.Current.Request.ApplicationPath + “/”;
    }

  • cooldev

    iis 7
    Guys,

    Great article its working fine on IIS 5 and IIS 6.Any ideas how to make it work on IIS 7.I am getting page not found error when i deploy my appication on IIS 7.any ideas..pls help

  • den2005

    Not working at Live server
    Hi,

    Anybody encountered this, I followed what is written here working at my local pc when I deployed/published it at Live server, url rewriting won’r work..Error 404?? Anybody how to fixed this, Live server using IIS 6.

    Please help..Thanks

  • mayuresh

    What should be the Start URL for this sample app
    I have configured the application to have the start URL as –
    http://localhost/RewriteModule/2006/12/10/ , but the HttpContext.Current.Request.Path is returning –
    http://localhost/RewriteModule/2006/12/10/Default.aspx

    The regular expression in the web.config doesn’t match the path returned. Please help

  • shekhar.kandregula

    url rewriting is working in local but it is not working in server any body plz
    Hi friends,
    i have used this code recently ,it is working in localhost but it is not working when i upload at the server.Can anybody plz tell me the solution,its very urgent.

    with thanks&regards,
    sekhar

  • shekhar.kandregula

    hi friends can any body tell the rule which will work in server
    hi friends can any body tell the rule which will work in server.

    i used below rule for url rewriting …

    <rule source=”(.+).blogs/(.+).aspx” destination=”blogsdisplay.aspx?name=$1&title=$2″/>

    but it is working in local but when im upload at server it ,it shows 404 error.

  • debayan

    Not working in server
    Hi friends , using the rewriting module,it work fine in local ,but it not work in server. Is any body has the solutions for it.
    Thank in advance.

  • thaiduong28

    Rewrite hompage
    i want to rewrite default.aspx
    like
    http://localhost:3959/Web/Default.aspx
    to http://localhost:3959/Web/Home

    when website starting

  • techie579

    IIS5.1
    Dear Sir

    I have tried the code. It’s nice working fine when i run the application from the file system. But if i try to run it from the IIS5.1 it’s not working.Why it’s not working. What the necessary things i have to do. Please help me to find the solution.

    Thanks

  • techie579

    URL Rewriting in IIS5.1
    Dear Sir

    When i host your files under C:inetpubwwwrootTestURL it’s working. The URL
    http://localhost/TestURL/ThisIsATest/Default.aspx
    is working without raising any error.

    But when i host the same files under
    C:inetpubwwwroot it’s not working.
    The URL http://localhost/ displays default.aspx
    page .
    The URL http://localhost/ThisIsATest/Default.aspx
    displays The resource cannot be found. What i hve to do inorder to solve this. Please help me. Please mail the solution to santosh579@gmail.com
    This is very urgent. We want to host it on the live server

    Thanks

  • shaileshbakadia

    Facing problem while deploying site on iis
    Dear Gaidar Magdanurov

    I am getting problem of 404 while deploying your url rewritting site its running perfect in local but when i try to deploy it on same machin on IIS with windows 2003 then it gives us error of 404 page not found. I am using version 5.2(Build 3790.srv03_sp_rtm.070216-1710: Service Pack 2).
    Can you or any body help me to resolved this issue. this is working very fine in local but giving problem at time of deploying. you can email me on shailesh.bakadia@hotmail.com

    Thanks

    Shailesh Bakadia
    shailesh.bakadia@hotmail.com

  • santhosh047

    problem
    when i configure in to iis i have to type
    http://localhost/web/default.aspx
    its works fine but when i type http://localhost/web
    shows error 404.
    what is the solution for this to access default.aspx directly without typing the file name

    santhosh kumar.G

  • manu4ever

    URL Masking
    Hi,
    Could you please help me on the below.
    How can I mask the URl, lets say from
    http://abc.ijk/xyz to http://xyz

    Please reply me.
    Regards,
    Manoj.

  • elitekrishnan

    More than one level of URL not working
    Hi,

    This URL rewriting is working fine for the one level of url structure, when we go to more than one level, it is not working.
    Example: If i am in 3rd level means, then it is taking the second level url’s page. Please suggest me to go for multi level url struucture.

    Please reply me.
    regards,
    Krishnan

  • elitekrishnan

    More than one level of URL not working
    Hi,

    This URL rewriting is working fine for the one level of url structure, when we go to more than one level, it is not working.
    Example: If i am in 3rd level means, then it is taking the second level url’s page. Please suggest me to go for multi level url struucture.

    Please reply me.
    regards,
    Krishnan

  • Bhaskar Munukuri

    This is artucle is not working for extension less urls
    Hi,

    This urlrewriting is not working for extensionless url. i was devoloped website in IIS6 and WindowsXp.
    This urlrewritng is not working.

    Please suggest me to devlope Extensionless url structure.

    Please reply me.
    Regards
    Bhaskar

  • elitekrishnan

    More than one level of URL not working
    Hi,

    This URL rewriting is working fine for the one level of url structure, when we go to more than one level, it is not working.
    Example: If i am in 3rd level means, then it is taking the second level url’s page. Please suggest me to go for multi level url struucture.

    Please reply me.
    regards,
    Krishnan

  • janaki

    configuration error
    i have used the same file , and i have add that referrences in BIN. Now , in Web.config if i write <modulesSection> then it gives me error , like Unorganized modulesSection in web.config file.

  • fewlines4biju

    Help Needed in URL Rewritting
    Hi Gaidar Magdanurov,
    Really this is a very nice article for URL rewritting in asp.net.

    Actually I have my own web site http://www.fewlines4biju.com . Here I am showing list of articles in gridview. And one user clicks on a particular link then it opes of page like http://www.fewlines4biju.com/TopicDetails.aspx?TopicID=34

    I want to implement URL rewritng so that the URL will come like http://www.fewlines4biju.com/How-to-implement-urlrewritting.aspx rather than with query string parameter. And this titlewill come from the Title Field in the database. I hope you would help me like what will be the rules for this and all. waiting for your reply.

  • vish007

    Not wroking on Live Server (The page cannot be found)
    Dear Gaidar Magdanurov,

    This is really very good article but the problem is its working good in localhost but on live server its not working, its showing “The page cannot be found”

    so which type of setting i have to do? Pls, guide me

    Thanks

  • wuxs

    I Translate your article into Chinese
    未经您的许可,我将您的这篇文章翻译为中文,并放在下面的地方,如果您有什么意见,或版权方面的要求,请给我发邮件:
    wwxs@163.com
    下面是译文地址:
    http://www.souty.cn/Admin/Knowledges/ShowKnowledge.aspx?id=d3c92130-4479-492c-9536-2f141a6557fd

  • lostapache

    fix for working locally but not on the server
    Like many of you it worked locally when using a virtual directory but not on the server.

    You need to change a line in RewriteModuleSectionHandler.cs

    In the file:
    _RewriteBase = HttpContext.Current.Request.ApplicationPath + “/”;

    Change to:
    _RewriteBase = HttpContext.Current.Request.ApplicationPath;

    Use the new dll in your application and it will work on the root.

    Cheers

  • lostapache

    getting it to work on root and virtual dir
    To get it to work on both the root and a virtual directory do the below changes in file RewriteModuleSectionHandler.cs

    Original Code:
    _RewriteBase = HttpContext.Current.Request.ApplicationPath + “/”;

    Change To:
    if (HttpContext.Current.Request.ApplicationPath.EndsWith(“/”))
    {
    _RewriteBase = HttpContext.Current.Request.ApplicationPath;
    }
    else
    {
    _RewriteBase = HttpContext.Current.Request.ApplicationPath + “/”;
    }

    Cheers

  • amitkumarrawat

    Unable to call usercontrol link button click
    I have a user control. In which there is signout button. When i click on signout. Then Sign Out button event not fired. Simply page refresh. Please help. 🙁 🙁

  • amitkumarrawat

    Unable to call usercontrol link button click
    I have a user control. In which there is signout button. When i click on signout. Then Sign Out button event not fired. Simply page refresh. Please help. 🙁 🙁

  • hareshambaliya

    do not work with godaddy
    Hi,
    Your article is nice.
    but I have problem with UrlRewriting. I got error on server. I am using IIS7 with shared hosting in godaddy.
    I got error page do not found. Please help me its urgent.

  • HarrySgss

    Great Work
    This is very nicely explained article on URL rewriting.

  • murraharri

    Very nice

    thanks a lot.

    Can i use your dll in the source in my code for url rewriting

  • dmaulik142

    How to write rule in web.config file for the given url?
    Hi Friend

    Excellent work u have done.

    I have url as below.
    http://www.levinsonjewelers.com/fine_jewelry.aspx?menu=4&catid=5&catsubid=2&productid=352&page=2

    Now I want to display above url as below into addressbar.

    http://www.levinsonjewelers.com/fine_jewelry/bracelets_whitegold.aspx

    Please let me know how to write in web config file for above url?

    Thanks
    Maulik

  • himabindu

    Good article
    Hi Gaidar Magdanurov,

    U have done an excellent work for this article.
    Congrats and thnx.

    I need some help

    I have an url like http://www.shopmate.com/Jewellery/Index.aspx

    Now i need to change it as
    http://www.shopmate.com/Shop1/Index.aspx

    That means
    the folder name Jewellery has to be changed to Shop1.

    How can i do this……..?

    Please let me know about this….

    Thanks in advance

  • Tanmaydeveloper

    Issue regarding IIS7.0
    Dear Gaidar Magdanurov,
    this is a great article of Url Rewriting I have also implemented in my solution on .net 3.5 plat form and it is running fine but its published code is not working on IIS 7.0 and IIS 5

    Please give me some description to work out

    thanks
    Tanmay Srivastava
    Tanmaydeveloper@gmail.com

  • Tanmaydeveloper

    errr i getting in iis 7
    HTTP Error 404.0 – Not Found
    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

  • nazojan

    please help
    HI everyone,
    I am hosting a site on 1and1.com, this URL rewriting app working very good on my localhost but when i try to host it on 1and1.com server I get an error that path not found, the Virtual Folders exemple is working fine but the othe exemple wtih Post.aspx to past the query strings they not working and I am geting the error message Error 404: NOT FOUND! I see some other people have a complaint about this above. Please help if anyone knows any thing about this.

    thnks,
    Naz
    webncyber@hotmail.com

  • swathinithya

    Not Working online
    when i have tried RewriteModule for url writing its working offline(Localhost) but when i have uploaded on webserver(online) its not working………Above i have seen the same problem even some are facing if you got the solution please do help me out…..thanks in advance .

  • kirkdm

    What about this scenario
    How do you implement a url rewrite for querystring that has spaces and you want the url to rewrite it with the “-” character in between each word for example

    http://www.mysite.com/some-text-here/default.aspx and
    http://www.mysite.com/article.aspx?id=some text here

  • daCodez

    VB.Net Conversion
    How would i convert the following to VB.NET anyone know?

    context.BeginRequest += New EventHandler(RewriteModule_BeginRequest)
    context.PreRequestHandlerExecute += New EventHandler(RewriteModule_PreRequestHandlerExecute)

  • daCodez

    Answer
    Hey all I answered my own question. For those who are interested in the answer it is:
    AddHandler context.BeginRequest, AddressOf Me.RewriteModule_BeginRequest
    AddHandler context.PreRequestHandlerExecute, AddressOf Me.RewriteModule_PreRequestHandlerExecute

  • Pankaj Mahajan

    Very good artical.Need some more info
    The given artical is very helpfull because it shows complete code and iis setting.
    Will you please suggest these setting for IIS 7.5 (window server 2008 R2).
    Also,these setting working fine in my case except root url.so can you please provide more information regarding this.
    Thanks

  • Munna.bhakta1001

    Problem after using rewrite Url
    hi i am using this url rewriter module but
    after using this module.
    my page load multipal time and data is going to loss
    .
    I not understand how can do.
    Url formate is
    <rule source=”CarReviewByExpertDetails/(.+)/(.+)” destination=”CarExReadMore.aspx?Manu_Name=$1&Mod_Name=$2″/>

  • Justin1978

    HttpContext.Current.RewritePath bug?
    Nice article, works fine except for one thing:

    I’m directing from a page1?foo=bar to a page2?hello=world.

    Your example joins te querystrings and uses HttpContext.Current.RewritePath to rewrite the path and the querystring. This works; the querystring now contains foo and hello (I use a foreach to loop trough HttpContext.Current.Request.QueryString)

    But: on the page that is redirected to, page2?hello=world, the HttpContext.Current.Request.QueryString only contains the query-variables from the request-uri, in this case only foo. Where’s hello?

    It appears that it is overwritten somewhere between the execution of the code in RewriteModule and the code on the redirectpage. Is this a bug? I’m breaking my head on how to solve this.

  • Bhavin

    Regular Expression
    Hello Sir,

    i am trying url rewriting in my application,i have added your code reference in my project and also done all web.config file setting also.

    My original url is below

    http://localhost:50820/cmgmt_28_04_2011/Quotes/mypage.aspx?catid=0

    in that whole url i have to make my url like below

    http://localhost:50820/cmgmt_28_04_2011/Quotes/General
    how can i do it.i have tried through your given test project as a reference but its give the error like “Resource can not be found.”
    can you please guide me in that.
    my web config rules like below
    <modulesSection>
    <rewriteModule>
    <rewriteOn>true</rewriteOn>
    <rewriteRules>
    <rule source=”(d+)” destination=”mypage.aspx?catid=$1″/>
    </rewriteRules>
    </rewriteModule>
    </modulesSection>
    if any error in that can you correct it and this code also working in live server if i ill upload it any setting for that also.
    waiting for your response as soon as early.

    Regards,
    Bhavin

  • Nidhi Thakrar

    this is not working withour virtual directory
    Hello,
    It’s really good article.
    But i am facing one problem.
    its working fine with virtual directory.
    But when i use this module without virtual directory ( i mean on direct wwwroot folder) its not working. Please guide what can i do?

  • tintu.augustine

    url rewriting issues
    i tried to implement this method in my site.url is changing but the page is not loading.showing
    Like this

    The resource cannot be found.
    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

    Requested URL: /hemi-sync/About_us

    can u please help me.

  • panda.dodt

    Rewrite with Server.Transfer method
    hi my friend.
    your idea is verry good, but i think it is not complete.i tested it, that’s ok. but when i use Server.Transfer(“url”) then it can not rewrite or wrong rewrite.
    so do you have a idea?

  • anis

    Problem Regarding Url Rewriting
    I have a problem in URL rewriting that its running properly in localhost but when i hit the default page in the server its not working.

    Plz Solve my Problem

  • gaidar

    ASP.NET 4 users should use Url Routing
    Hello dear friends,

    It’s been ages since I wrote the article and I am still getting questions and comments on the solution. Though, in case you use ASP.NET 4 I propose to use in-built Url Rounting – http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

  • anilbabu.m

    URL REDIRECTION
    What is URL REDIRECTION?How can i use this concept in my .net?
    my task is i have generated one url like this “http://example.com/”
    I am passing one pearameter like “http://example.com/Empno=1”
    I want to display Ename in Database table that corresponding “Empno”
    plz Help me Give me one simple example

    I am new this concepts
    plz Help me send source code to my mailID:anilbabu.m@hotmail.com

  • abhitheflame

    Not working On Server
    Your code working properly at local machine but not on a server. I have configure the isapi in iis 6.0 but still not worked.Please give me a solution.

  • changtotbung1105

    Is not match
    Hi everybody! the commend: if (match.Success) is alway false! how can you help me?

  • abhitheflame

    How can I create rules from code behined and then add that rules in config file
    I want to create some rules dynamically from my code behined. Is there any way to achieve this/.

  • abhitheflame

    How can I create rules from code behined and then add that rules in config file
    I want to create some rules dynamically from my code behined. Is there any way to achieve this/.

  • amir490

    IIS 6 Website Solved Problem
    Hi
    Thanks For your solution
    i found Problem On the URL rewriting problem in website but this worked on virdir
    the problem is in RewriteModuleSectionHandler.cs

    _RewriteBase = HttpContext.Current.Request.ApplicationPath + "/";

    and this section

    this part of the code just return in virtual directory but in the website return empty

    you can use http://michaelkappel.com/examples/CSharp/Helper/ApplicationPath.htm

    for return ApplicationPath()

    and fix it to

    _RewriteBase = MichaelKappel.Net.CSharp.Helper.ApplicationPath() + "/";

    With the hope of success

  • alfredmarshall

    Dedicated .net Programmer
    Hello,i have gone through your article and it is very good and impressive you have maintained it very properly and after studying the matter very well. Thanking you

  • ashishr

    Rewrite URL
    I’ve URLs in my website and which are using query string values For example: http://foo.com/xyzPage.aspx?barvalue=yehaa
    to
    http://yehaa.foo.com/
    Please suggest how it can be accomplished without actually creating subdomains on server ..
    I’ve IIS 7.5 installed on server machine and using Asp.net 4.0.

  • ashishr

    Rewrite URL
    I’ve URLs in my website and which are using query string values For example: http://foo.com/xyzPage.aspx?barvalue=yehaa
    to
    http://yehaa.foo.com/
    Please suggest how it can be accomplished without actually creating subdomains on server ..
    I’ve IIS 7.5 installed on server machine and using Asp.net 4.0.

  • ashishr

    Rewrite URL
    I’ve URLs in my website and which are using query string values For example: http://foo.com/xyzPage.aspx?barvalue=yehaa
    to
    http://yehaa.foo.com/
    Please suggest how it can be accomplished without actually creating subdomains on server ..
    I’ve IIS 7.5 installed on server machine and using Asp.net 4.0.

  • guptagaurav23techgig

    Not Working After Publish Or in new Virtual path
    it is working fine when i run it on Visual studio. but i publish it then it not working…

    i have iis 7.5…

    one more thing i noticed… it mainly do not work when we create virtual directory under root directory…

    For example:–

    i have a website…www.mywebsite.com

    then it working

    but when i created hindi virual directory under above website root then it do not work for

    http://www.mywebsite.com/hindi

    as new web application

  • srinivasvanka

    rewritting url
    Hello,
    Actually i have a doubt regarding Rewritting URL using asp.net.

    https://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/

    By using above link am able to produce the required url rewritting through Visual studio 2005 bug unfortunately i am unable to produce it in VS 2013 .

    Have u tried it in 2013 or 2005,,
    can u give me any suggestions..

    thanks