January 28, 2021
Hot Topics:

How to Implement and Utilize URL Rewriting with ASP.NET

  • By David Consdorf
  • Send Email »
  • More Articles »

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.

Page 3 of 4

This article was originally published on October 8, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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