IIS7: URL Rewrite Extension on Windows Server 2008By Dave Lawlor
One of the greatest advantages that Apache server has had over the years, compared to prior versions of IIS, is the ability to use mod_rewrite to change URLs on the fly very easily.
While there were options for this on earlier versions of IIS via the ISAPI rewrite modules, they were problematic and resource intensive for sites with a lot of traffic and/or custom code.
Now you may be wondering why this is a big deal? Well if you are running an intranet site, it really isn’t unless you want your users to see nice friendly URLs they can remember. The beauty of this comes into play when you are trying to rank your website for certain keywords on the major search engines. It is much easier if the URL of the webpage contains the keyword than if it is just some random number because of the content management system you might be using.
Well IIS7 now has an answer to that problem with the release of the URL Rewrite extension. This has been in testing for several months, but just recently went to a “Go Live” status, which means it is clear for production environments.
In this article I am going to walk through installing and configuring the URL Rewrite module on a Windows 2008 Server running IIS7.
Install URL Rewrite
Let’s start by installing the URL rewrite extension on our server. You will be able to download the URL Rewrite from the following locations depending on your version of server:
- Microsoft URL Rewrite Module for IIS 7.0 Go Live (x86)
- Microsoft URL Rewrite Module for IIS 7.0 Go Live (x64)
Once you go and download the file you need we can start. I am going to assume that you already have IIS7 installed on the server you are working on.
1. Double click on the version of the file you downloaded.
2. The license agreement is up first, go ahead and accept the terms by placing a check in the box provided then click Install.
3. You’ll see a status bar as it installs itself and then you will see a window that tells you the install is now complete. Click on Finish to close.
That’s all there is! Refreshing to have an easy install isn’t it? Don’t worry all the hard stuff is on the back end for this module.
URL Rewrite Usage and Configuration
There are many different ways to use the URL Rewrite module. Some of these are very complex using programming regular expressions to match rules, and others are straight static mappings.
You can also use the URL Rewrite module to make all your urls lowercase. Let’s take a look at a few examples of how to use these.
URL Rewrite Maps
This is one of the more straightforward usages of the URL Rewrite module as it doesn’t require complicated matching in the rules, you are basically doing mapping from one name to another.
Some applications have long file names that are not really friendly for the users or search engines. For this example I’m going to show you how to use a search engine friendly url to map to the unfriendly one.
We will use a friendly URL like this one:
http:// localhost /iis7/urlrewrites/
And map it to the unfriendly url on the backend:
http:// localhost /index.asp?cat=iis7&topic=urlrewrites
1. Start IIS Manager.
2. Left click on the site you want to work with and then left click on URL Rewrite in the center pane.
3. In the right pane, click on Add Rule.
4. There are 4 rule templates loaded by default. For our example we are going to choose Rule with rewrite map.
5. In the Choose the rule action: pick Rewrite in the drop down, and then in the Choose the rewrite map:, you can either choose an existing one or type in a name of the new one.
In our test I am simply going to name this rewrite map Demo. When you’re done click Ok.
6. Now you are in the Edit Rewrite Map mode, in the right pane click Add Mapping Entry.
7. Now the Add Mapping Entry window opens up and we can add the original page value and the new page value.
For this article in the original value we will use: /iis7/urlrewrites/ and in the New value we will use /index.aspx?cat=iis7&topic=urlrewrites and then click Ok.
8. In the center pane you can now see your static mapping. If you have others you need to map then you can add them in here by following the steps above.
9. I’m going to show the results using a test page that will call the destination url from the server to show what it would call on the backend.
I am going to open my browser and go to http://localhost/iis7/urlrewrites/ as you can see in the screenshot the backend call would have gone to the url http://localhost/index.aspx?cat=iis7&topic=urlrewrites.
In this way you can make unfriendly urls from applications such a Joomla appear much friendlier to search engine and users.
URL Redirection in IIS7
Another useful feature of the URL Rewrite module is the ability to quickly and easily do redirections properly with permanent or temporary status (301 or 302). Very useful if you are changing page names and want your users and search engines to easily find the new content.
You use a 301 if it is going to be a permanent change or if it is just temporary then use a 302. Again we will use a rewrite map for this. In this example we will do the following:
Redirect the page: http://localhost/oldpage.htm
To the page: http://localhost/newpage.htm
1. In IIS Manager go the site you want to define redirects on and then click on URL Rewrite in the center pane.
2. Click on Add Rules in the right pane.
3. For this demonstration we are going to choose Rule with rewrite map and click Ok.
4. In the dropdown choose Redirect and for the Rewrite Map name type in Redirect Demo, then click OK.
5. Now click on Add Mapping Entry in the right pane.
6. For the Original Value use /oldpage.htm and for the New Value use /newpage.htm then click OK.
7. You can now see your mapping in the Rewrite Map table as shown below:
8. By default these will be permanent redirects (301), which should be fine for most uses, but if you know this is a temporary page for something like a seasonal sale, then you can set this to be a temporary (302) redirect so the search engines don’t lose the original page from their index.
To do this click on Back to Rules.
9. Choose Redirect rule 1 for Redirect Demo and then click Edit in the right pane.
10. Go to the bottom of the Edit Rule page and you will see Redirect Type:. In the dropdown go ahead and choose the type of redirect.
For this example I am going to leave it at 301, but if you needed to you could change it to one of the others. After choosing your type, click Apply in the right panel.
11. Let’s go ahead and test this by pointing our browser at http://localhost/oldpage.htm and hitting Enter.
12. Your url should have been redirected to http://localhost/newpage.htm without any intervention or error message from the server.
These two parts of the URL Rewrite module just scratch the surface of this powerful add on. While these examples are mostly manual mappings the true beauty of this add on is that you can assign these rules using programming functions on the fly, but that is beyond the scope of this article.
I will be using this module in a few upcoming articles having to do with installing popular open source CMS software such as WordPress and Joomla.
Ready to test your skills in Windows Networking Infrastructure? See how they stack up with this assessment from Smarterer, the newest addition to the Pluralsight family. Start this Windows Networking Infrastructure test now
About the Author
Dave Lawlor (MCTS, MCP, A+) has been working in the IT field since leaving the U.S. Army in 1996. Working his way up from printer hardware repair to running a corporate datacenter for a multinational corporation, Dave has seen many environments throughout the years. Focusing on web sites and search engine optimization the last few years, with the release of Server 2008 it has renewed his passion for the Wintel platform and server technologies. David also runs Windows-Server-Training.com where he posts free videos and walk-throughs for a variety of server technologies. David currently works as a freelance technical consultant and writer for a variety of companies in the Chicago area.
Author's Website: http://www.DaveLawlor.com