http://www.developer.com/

Back to article

How to Implement and Utilize URL Rewriting with ASP.NET


October 8, 2007

Introduction

Smart URL rewriting can add usability and professionalism to any web site. It is also an important factor in helping web sites control how they are indexed by search engines. In this article, I will walk you through a simple implementation of URL rewriting for ASP.NET and some solutions to common problems. I will also discuss why URL rewriting is important and how URL rewriting can be used to enhance your web site.

What Is URL Rewriting?

URL rewriting is the process of taking an incoming URL request and rewriting it to another URL that the web site can process. For example, if the URL comes in through the browser as "www.mysite.com/UserProfile/1.aspx" it may be rewritten to another URL like "www.mysite.com/UserProfile.aspx?ID=1" that your site can better read.

The ability to rewrite URLs is quite useful because it allows you to do things such as improve search engines' ability to read and index your site, change your web site structure without requiring users to change their bookmarks or other web sites to change their links, improve your web site's security, and generally making your site more usable and professional. I will go over each of these items in more detail the "How to Utilize URL Rewriting" section of this article.

How to Implement URL Rewriting with ASP.NET

URL rewriting can be implemented programmatically. The Context.RewritePath() method allows you to programmatically rewrite the processing path of an incoming URL. Once rewritten, the system will continue executing the request using the new path.

In the Application_BeginRequest() method of the Global.asax file, you need to add code to read the incoming path, match it to one or more URL rewriting rules, and then rewrite the path for further processing. See the example below; it implements the following URL rewrite rules:

Input URL Actual URL
~/UserAccount/OldUrl.aspx ~/UserAccount/NewUrl.aspx
~/UserAccount/{UserID}.aspx ~/UserAccount.aspx?ID={UserID}

Table 1: Context.RewritePath() Example: URL Rewrite Rules

void Application_BeginRequest(object sender, EventArgs e)
{
   String path = Request.Url.ToString();

   if (Regex.IsMatch(path, "/URLRewriting/OldUrl.aspx",
       RegexOptions.IgnoreCase))
   {
      Context.RewritePath("/URLRewriting/NewUrl.aspx");
   }
   else if (Regex.IsMatch(path, "/URLRewriting/UserAccount/(.+).aspx",
            RegexOptions.IgnoreCase))
   {
      String idString =
         path.Substring(path.LastIndexOf('/') + 1,
                        path.Length - path.LastIndexOf('/') - 6);
         Context.RewritePath("/URLRewriting/UserAccount.aspx?id=" +
                             idString);
   }
}

Listing 1: Using Context.RewritePath() to perform URL rewriting

In this example, every time a new request is processed it will first run through the Application_BeginRequest(). The URL path input is retrieved using the Request.Url property and then the web site URL rewrite rules are applied by using regular expressions to match against expected inputs and to rewrite them as paths to the pages in your site.

A More Robust Example Using UrlRewriting.NET

Using the Context.RewritePath() method to programmatically rewrite URLs works fine when done on a small scale with only a limited set of rewrite rules, but larger sites often will have many rules for URL rewriting. Coding all of those rewrite rules manually can be a cumbersome and error-prone task.

A better solution is to use an HttpModule to dynamically handle URL rewriting rules from the web.config file. Instead of writing the HttpModule to handle this task yourself, there are several good free versions already out there. Here are a few of them:

For this article, I will walk you through a simple example using URLRewriting.Net. To begin using URLRewriting.Net, first download the binaries from the URLRewriting.Net web site and put them in your websites/bin folder.

Next, add the following configuration settings to the web.config file:

<configSections>
   <section name="urlrewritingnet" requirePermission ="false"
            type="UrlRewritingNet.Configuration.UrlRewriteSection,
            UrlRewritingNet.UrlRewriter"  />
</configSections>

<system.web>
   <compilation debug="true"/>
   <authentication mode="Windows"/>
   <httpModules>
      <add name="UrlRewriteModule"
           type="UrlRewritingNet.Web.UrlRewriteModule,
                 UrlRewritingNet.UrlRewriter" />
   </httpModules>
</system.web>

Listing 2: Configuring URLRewriting.Net

Now, in the web.config file you can add the following section and enter in your own URL rewrite rules. See the example below; it implements the following URL rewrite rules:

Input URL Actual URL
~/UserAccount/{UserID}.aspx ~/UserAccount.aspx?ID={UserID}
~/Movies/{MovieType}.aspx (Defined Set) ~/Movies.aspx?MovieType= {MovieType}

Table 2: URLRewriting.Net Example: URL Rewrite Rules

<urlrewritingnet
   rewriteOnlyVirtualUrls="true"
   contextItemsPrefix="QueryString"
   defaultPage = "Default.aspx"
   defaultProvider="RegEx"
   xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
   <rewrites>
      <add name="UserAccount"
           virtualUrl="^~/UserAccountV2/(.+).aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/UserAccountV2.aspx?id=$1"
         ignoreCase="true" />
      <add name="Movies_Action"
           virtualUrl="^~/Movies/Action.aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Movies.aspx?MovieType=Action"
         ignoreCase="true" />
      <add name="Movies_Drama"
           virtualUrl="^~/Movies/Drama.aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Movies.aspx?MovieType=Drama"
         ignoreCase="true" />
      <add name="Movies_Comedy"
           virtualUrl="^~/Movies/Comedy.aspx"
         rewriteUrlParameter="ExcludeFromClientQueryString"
         destinationUrl="~/Movies.aspx?MovieType=Comedy"
         ignoreCase="true" />
   </rewrites>
</urlrewritingnet>

Listing 3: Configuring Rewrite Rules with URLRewriting.Net

As you can see, when using an HttpModule like URLRewriting.Net you can simply insert your web site's rewrite rules into the web.config file one by one without any code at all. This makes it very easy to manage the URL rewrite rules for your web site, large or small.

Also note that although the example here is pretty straightforward, URLRewriting.Net is a robust program with many more options than I go over here. Full documentation and a sample application can be found on the URLRewriting.Net web site.

Handling Postbacks

One potential problem that often arises when using URL rewriting methods with ASP.NET is that the clean input URL is not maintained on a page postback to the server. For example, you may enter a site using your nice clean URL such as "~/Movies/Action.aspx" but then when you click a button that performs a postback to the server, the URL will change back to the actual URL, "~/Movies.aspx?MovieType= Action".

The root of the problem lies with how the <form runat="server"> tag renders the action property. It uses the actual URL instead of the input one.

A simple way to remedy this problem is to write your own version of the form tag by extending the existing form tag. Then, you can set the action as the input URL instead of the rewritten one. See the example here:

public class RewriteForm : HtmlForm {

   protected override void
      RenderAttributes(System.Web.UI.HtmlTextWriter writer) {

      // Name
      writer.WriteAttribute("name", this.Name);
      base.Attributes.Remove("name");

      // Method
      writer.WriteAttribute("method", this.Method);
      base.Attributes.Remove("method");

      this.Attributes.Render(writer);

      // Action
      String action = Context.Request.RawUrl;

      if (action != null) {
         writer.WriteAttribute("action", action);
      }
      base.Attributes.Remove("action");

      // ID
      if (base.ID != null) {
         writer.WriteAttribute("id", base.ClientID);
      }
   }
}
Listing 4: Custom form tag that can handle URL rewrite postbacks

Using the code above, you now can use the <MyTags:RewriteForm ID="form1" runat="server"> tag, instead of using the standard <form id="form1" runat="server"> tag,. This will keep the URL consistant even during postbacks.

Handling Links and Image/CSS URLs

One important note when using URL rewriting is that relative links, images, and CSS may stop working because the relative references will now be relative to the input URL and not the actual URL.

For example, if the home page of a web site, "/home.aspx?lang=en", has a relative reference to "mysitelogo.jpg", but the page is requested using the URL "/en/home.aspx" with an imaginary "/en" folder to indicate that the user wants the English version of the page, the relative link for the "mysitelogo.jpg" becomes "en/mysitelogo.jpg"; this is incorrect and the image will not display correctly.

To ensure that images and links point to the correct URL, be sure you specify an absolute URL such as "/mysitelogo.jpg" or "www.mysite.com/mysitelogo.jpg". Another option when using ASP.Net tags is to use "~/" in front of your links (in other words, "~/mysitelogo.jpg"). This will automatically render the correct path to your file or link.

Changing the File Extension

One of the more interesting changes you can make to your site's URL is to change the extension of your pages. For example, it is often desirable to change the extension ".aspx" to some custom extension, possibly ".x" or ".mysite" or even ".jsp" or ".php". This may be done for security reasons or just to add to the aesthetic quality of your website.

To change the extension of your site's pages, just use the methods described in the URL rewriting examples above to rewrite URLs containing ".mysite" to point to pages with an ".aspx" URL. This can be made even easier when using a module such as URLRewriting.Net. Instead of having to specify each and every page you want to change the extension on, you can add a rewrite rule at the end of the list that catches all pages not already caught and converts them. For example, rewrite "~/(.+).mysite" to "~/(.+).aspx". This method works because the URL rewrite rules are executed in order and putting this rule at the end of the list will act as a catch all.

Also, when using alternate file extensions, make sure you configure the new extension in IIS mappings. Note that the ASP.NET-related extensions (".aspx", ".asax", ".config", ".cs", and so forth) are all mapped to the aspnet_isapi.dll ISAPI extension. Add your new extension with the same settings.

Figure 1: Configuring IIS Mappings

How to Utilize URL Rewriting

So, now that you have spent the time to figure out how to implement URL rewriting, look at how you can utilize this ability to improve your web sites.

1. Improve search engine's ability to read and index your site's pages

Search engines crawl sites and index them based on the URL. For many dynamic database driven sites that depend on dynamic URLs with URL parameters such as an ID parameter (as in. "www.mysite.com/UserProfile.aspx?ID=1"), search engines are not able to fully index your site. By changing the URL to something like "www.mysite.com/UserProfile/1.aspx", you can help the search engine index your site more easily.

Search engines also determine your page's relevance based on the keywords in your URL. Sometimes, you may want to change the URL to better reflect the content of the page without having to change your sites directory structure the actual page name.

There are also many other ways URLs can affect the way your site is indexed by search engines. If you would like to learn more, here is a good article to follow up with: How URLs Can Affect Top Search Engine Rankings.

There are no hard and fast rules for making your site search well on all of the big search engines, but being aware of how search engines work and playing around with how your site is set up can make a big difference. Also, be sure to stay honest to your site's content when working with search engine optimization. The idea is to help search engines index your site, not to trick them. Search engines don't like to be tricked and the last thing you want is to be black listed on the major search engines.

2. Change your site structure without requiring users to change their bookmarks or other web sites to change their links to your web site

If you want to change the name of a page or change the folder structure in which you store your pages, it will mess up the browser bookmarks and web site links people have made for that page. One possible solution is to use URL rewriting to transparently redirect the user to correct page.

So, for example, if you have the page "www.mysite.com/main/sitecontacts.aspx" users have bookmarked and you want to change it to "www.mysite.com/contacts.aspx", you can just set up a redirect rule that will return the page for the URL, "www.mysite.com/contacts.htm", when the URL, "www.mysite.com/main/sitecontacts.htm" is entered in the browser.

3. Improve the security of your web site

URLs can contribute to security threats because they expose the inner workings of your web site.

For example, by using the ".aspx" prefix, you are exposing that you are using ASP.NET as opposed to some other framework such as JSP or PHP. This can give a potential hacker clues as to how to attack your site. URL rewriting can help by changing the prefix to something more generic like ".x" or ".mysite". This will help keep potential hackers guessing.

Another security concern is that displaying your site directory and URL parameters in a URL can give a hacker vital information for attempting front-end attacks. With this information, they can potentially figure out a security hole in your site by changing your parameters around or accessing parts of your web site they are not supposed to know about. URL rewriting can help by hiding vital URL parameters and changing your directory structure.

Also, note that as far as security is concerned, URL rewriting is only one part of a site's complete security package. Simply hiding security holes doesn't mean that they aren't there, so be sure to test your site's security thoroughly.

4. Improve the usability and professionalism of your site

Long non-user friendly URLs can be a usability issue. Users will not always link into your site from another link. Users manually type in the URL for sites more than you might think. If the users have to type in a long and/or messy URL, it may be a deterrent and some users will decide it is not worth it. Another thing to consider is the ease with which people can talk about your site. A long URL that is hard to say will not be communicated as readily as a concise and meaningful one. Long URLs also may cause problems when a user emails a friend your sites URL. Sometimes, long URLs can be difficult to fully highlight and they may not be copy/pasted in their entirety. If this is the case, the emailed link will not work and you may lose a potential user.

Whatever the case, it tends to be a good idea to keep URLs simple and concise. URL rewriting can help do this transparently without having to change the underlying directory structure and page names.

Nice-looking URLs can also add to a site's professionalism and overall look and feel. It also gives you a chance to show off that you know what you are doing. Nothing says hobbyist web site like a messy URL structure.

Conclusion

The methods and ideas discussed here are only part of a complete solution to a great web site, but I hope this article has given you a comprehensive look into how and why URL rewriting can be used to enhance your web site. There are many things to take into consideration when building the perfect web site and URL rewriting is a good tool to have in your arsenal. With a solid plan and hard work you will find success. Happy coding!

Code Examples

To download the example code in this article, click here: Download Code Examples.

About the Author

David Consdorf graduated with a Bachelor's Degree in Computer Science from the University of Illinois Urbana-Champaign and now resides in Chicago, Illinois. For the last three years, he has been developing ASP.NET web applications as a software consultant for Crowe Chizek and Company LLC. Crowe Chizek and Company LLC is a Microsoft Gold Certified Partner.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date