Import Apache mod_rewrite Rules

Introduction

The Internet Information Services 7 (IIS 7) 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 guides you through the process of importing several mod_rewrite rules into an IIS configuration file by using the Import Rules feature provided in the URL Rewrite Module.

Set Up a Walkthrough Scenario

To test that the rules that are converted from mod_rewrite work correctly, you can implement a common scenario of enforcing canonical host names for the Web site. You can force the use of www.mysite.com instead of mysite.com, so when a request is made using a host name other than www.mysite.com, you redirect the request to a canonical hostname.

1. Start IIS Manager, and then click Default Web Site.
2. In the Actions pane, click on Bindings, and add a new http binding for port 8088.

Figure 1: Add a new binding



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

127.0.0.1 www_mysite_com
127.0.0.1 mysite_com

Note you are using “_” instead of “.” for domain separators. This is to prevent a Web browser from trying to resolve the domain name by using a Domain Name System (DNS) server.

4. Verify that the host names were setup correctly by opening a Web browser and going to the sites http://www_mysite_com/iisstart.htm and to http://mysite_com/iisstart.htm.

Convert mod_rewrite Rules

The Apache mod_rewrite rules to use for enforcing canonical host names 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, start IIS Manager, and then click URL Rewrite.

Figure 2: Click URL Rewrite

Paste the mod_rewrite rules into the Rewrite rules text box.

Figure 3: Rules to import

The Tree View in the Converted rules group box shows the result of the conversion. You can also switch to the XML View to see how the rules are stored in the Web.config file.

Figure 4: Converted rules

If you switch back to Tree View and select a node in the Tree View, the corresponding mod_rewrite rule directive in the Rewrite rules text box is highlighted.

Figure 5: Tree view

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

Figure 6: Rename

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 Apply to save the converted rules to the Web.config file, and then click Back to rules.

Figure 7: Back to Rules

Test the Converted Rules

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

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

In both cases, the Web browser is redirected to http://www_mysite_com/iisstart.htm.

Also, if you try either of these URLs:

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

The Web browser gets redirected to http://www_mysite_com:8088/iisstart.htm.

Note that the rules that were imported from mod_rewrite enabled the enforcement of canonical host names for a Web site. You can see other examples of Apache mod_rewrite rules.

Disclaimer

 IMPORTANT - 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) are not recognized or are converted incorrectly.

Related Content

Comments

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.

May 20 2009 by GCC_Carl

The rules are imported into the web.config file for the web site.

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!

Jun 06 2009 by ruslany

Submit a Comment

You must Log In to comment.

Microsoft Communities