Importing Apache mod_rewrite Rules

Author: Ruslan Yakushev

Published on May 30, 2008 by ruslany

Updated on June 22, 2009 by ruslany

Average Rating  Rate It (1)

RSS

IIS 7.0 URL rewrite module provides rule importing functionality that greatly simplifies the process of converting Apache mod_rewrite rules to IIS URL rewrite rules. This walkthrough will guide you through how to import several mod_rewrite rules into an IIS configuration file by using the “Import Rules” feature provided in URL rewrite module.

Setting up a walkthrough scenario

To test that the rules, converted from mod_rewrite, work correctly we will implement a common scenario of enforcing canonical hostnames for the web site. We want to force the use of www.mysite.com instead of mysite.com, so when request is made using a host name other than www.mysite.com we want to redirect the request to a canonical hostname.

To setup the scenario follow these steps:

1. Open the IIS Manager and click “Default Web Site”
2. In the “Actions” pane  click on “Bindings…” and add a new http binding for port 8088:


 
3. Using Notepad open the file %SystemDrive%\windows\system32\drivers\etc\hosts and add these two lines at the end:

127.0.0.1 www_mysite_com
127.0.0.1 mysite_com

Note that we are using “_”, instead of “.” for domain separators. This is to prevent web browser from trying to resolve the domain name by using a DNS server.

4. Verify that the host names were setup correctly by opening web browser and making requests to http://www_mysite_com/iisstart.htm and to http://mysite_com/iisstart.htm

Converting mod_rewrite rules

The Apache mod_rewrite rules to use for enforcing canonical hostnames are:

#For sites running on a port other than 80:
RewriteCond %{HTTP_HOST}   !^www_mysite_com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*)         http://www_mysite_com:%{SERVER_PORT}/$1 [L,R]

#And for a site running on port 80
RewriteCond %{HTTP_HOST}   !^www_mysite_com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www_mysite_com/$1 [L,R]

To convert these rules to IIS URL rewrite specific format, open IIS Manager and click “URL Rewrite”:


 
Paste the mod_rewrite rules into the “Rewrite rules” text box.


 
The tree view in the “Converted rules” group box will show the result of the conversion. You can also switch to the “XML View” to see how the rules will be stored in web.config file.


 
If you switch back to “Tree View” and select a node in a tree view the corresponding mod_rewrite rule directive in the “Rewrite rules” text box will be highlighted.


 
Note that during rules conversion the rules were assigned with default names. You can change the names to convey some meaningful information about what the rule does. To rename a rule:- select it in the tree view and then right-click to view a context menu:


 

Change the name of the first rule from “ImportedRule1” to “Redirect to www_mysite_com:non-80”. Change the name of the second rule from “ImportedRule2” to “Redirect to www_mysite_com:80”

Click on “Apply” to save the converted rules to web.config file and then click ”Back to rules” action:

Testing the converted rules

To test that the rules, imported from mod_rewrite format, work correctly, open a web browser and request either one of the following URLs:

http://localhost/iisstart.htm
http://mysite_com/iisstart.htm

In both cases you will see that the web browser is redirected to http://www_mysite_com/iisstart.htm:
 
Also, if you try any of these URL’s:

http://localhost:8088/iisstart.htm
http://mysite_com:8088/iisstart.htm

you will see that the browser gets redirected to http://www_mysite_com:8088/iisstart.htm  

Summary

In this walkthrough, you have learned how to import Apache mod_rewrite rules into the IIS configuration, so that they can be used by the URL rewrite module. The rules that were imported from mod_rewrite enabled the enforcement of canonical host names for a Web site. For other examples of Apache mod_rewrite rules, see http://search.live.com/results.aspx?q=mod_rewrite+examples.

Disclaimer

The URL rewrite module tries to convert Apache mod_rewrite rules to functionally equivalent IIS URL rewrite rules. However, not all mod_rewrite rules can be converted because of architectural differences between Apache and IIS. It is highly recommended that you study a mod_rewrite rule set until you understand its functionality before you begin the conversion process. Then, after converting to IIS URL rewrite rules, review and test the result of the conversion to make sure that the corresponding IIS rewrite rule set provides the same URL rewriting logic.

Note that only rules that follow Apache mod_rewrite syntax can be converted. Any other formats of rewrite rules (for example, ISAPI_Rewrite, Ionic ISAPI Rewrite, IISRewrite, and others) will not be recognized or will be converted incorrectly.

Related Content

Comments

  1. Submitted on May 20 2009 by
    GCC_Carl
    Are the mod_rewrite rules attached to the site in IIS, or put in the published site's folder? I do multiple site publishes, and will choose the most recent folder, and assign it as the home directory; that way the site isn't down or unavailable while it's being published. I'm looking into using cleaner URLs, but I don't want to have to copy and re-add my rewrite rules each time I publish, if possible.
  2. Submitted on Jun 06 2009 by
    ruslany
    The rules are imported into the web.config file for the web site.
  3. Submitted on Jun 06 2009 by
    ruslany
    PLEASE POST YOUR QUESTIONS ABOUT THE MODULE HERE: http://forums.iis.net/1152.aspx. YOU WILL GET MUCH FASTER RESPONSE!

You must Log In to comment.