One of the tools I use with IIS 7.5 and 8.0 as a web administrator and designer to add functionality to applications like content management systems, document management systems and static web sites is to use URLRewrite.
This module is freely available from Microsoft and allows for creation of unique rules and processing of incoming and outgoing requests on the fly.
So what can it do? Most often, it allows for very simple handling of URL requests using simple to advanced rules that are accessible via the IIS Manager tool or by direct editing of the web.config file hosted under the web site for which the rules will be applied.
The most common rules URLRewrite is used for is for redirects based on one or more criteria.
In this first article, we will test two specific cases
- Single URL Redirect.
- Multiple Conditional Match Redirect
Single URL Redirect
Imagine a case where you want a specific page that is available on the web root of a site to no longer be accessible. You may want to use the page later, but you simply need a quick and dirty rule to redirect the page until you need it again later.
Since I use PHP often, I occasionally like to use a classic page called “phpinfo.php” to look at the site environment variables for my site. But it is dangerous to leave this file unprotected since it can give anyone a deep look into your site specifics. So, it makes sense to add a rule to deactivate access to the file until there is a need to use it.
The first step is to insure that URLRewrite is installed. To do so, make sure you have installed IIS on your system along with the IIS Manager tool and then install the “Web Platform Installer” per the instructions from Microsoft.
This tool allows IIS to install a wide range of add-ons from PHP to Perl and of course, URLRewrite 2.0. Simply follow the instructions and once it is installed, then launch Web Platform Installer. See the red colored icon under “Management” to launch the installer:
Launch the Web Platform Installer.
Double-Click the icon to launch URL Rewrite and examine the screen. There will be no rules installed and the utility is waiting for it’s first rule to be added.
Let’s get back to the phpinfo.php rule.
We want to take any request coming into this site where the file “phpinfo.php” is referenced and to redirect it to another web site.
There are two ways to do this:
- From the IIS Manager screen with URL Rewrite Module administration.
- Or… by editing the web.config file at the site root.
Let’s start by doing the rule addition with the first option via the GUI. Add a new rule by clicking “Add Rule(s)” at the top right of the screen.
Next, choose “Blank rule” and press “OK”.
Give the rule a name. In this case called it “Example of Redirect”.
Make the following settings:
- Under “Match URL” set “Requested URL:” to “Matches the Pattern” and “Using” to “Exact Match”
- In the “Pattern” box, type “phpinfo,php” or a file in your web root that you wish to redirect.
- Under “Action”, chose “Action type” of “Redirect”
- Under “Redirect URL:” enter a URL of choice. I entered http://www.google.com for the URL to redirect to.
- Now click “Apply” on the top far right.
- The rule is built and ready to use.
- Enter the URL http://localhost/phpinfo.php (or whatever site url is appropriate) and your rule should activate.
If you have trouble making the rule work, go back and check your entries for typos or not selecting the right drop down.
Now, go check your web.config file by editing with notepad or similar editor. You should see the following fragment:
<rule name=”Example of Redirect” enabled=”true” patternSyntax=”ExactMatch” stopProcessing=”true”>
<match url=”phpinfo.php” />
<conditions logicalGrouping=”MatchAll” trackAllCaptures=”false” />
<action type=”Redirect” url=”http://www.google.com” />
</rule>
You can also just cut and paste this into the web.config file as follows:
<?xml version=”1.0″ encoding=”UTF-8″?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name=”Example of Redirect” enabled=”true” patternSyntax=”ExactMatch” stopProcessing=”true”>
<match url=”phpinfo.php” />
<conditions logicalGrouping=”MatchAll” trackAllCaptures=”false” />
<action type=”Redirect” url=”http://www.google.com” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Multiple Conditional Match Direct
Let’s try a different rule. First, we want to go to URLRewrite at the site list of icons and double click it. Let’s deactivate the first rule we did above. Highlight “Example of Redirect” and then on the right Actions menu, click “Disable Rule”. This is done to prevent it from firing because we are trying a new way to disable the phpinfo.php file from being accessed and to redirect it.
Like we did before, add a new blank rule. Call this one “Example of Redirect with multiple conditions”.
Enter the following:
- Under “Match URL”, select “Matches the Pattern” for Requested URL and “Regular Expressions” for “Using:”
- In the “Pattern” box enter (.*)
- Under Conditions click “Add”
- For Condition input type {SCRIPT_NAME}
- For Check if input string: “Matches the Pattern”
- Under pattern, enter “phpinfo.php”
- Click OK to close.
Add a new blank condition rule:
- Under “Match URL”, select “Matches the Pattern” for Requested URL and “Regular Expressions” for “Using:”
- In the “Pattern” box enter (.*)
- Under Conditions click “Add”
- For Condition input type {SCRIPT_NAME}
- For Check if input string: “Matches the Pattern”
- Under pattern, enter “somefile.html”
- Click OK to close.
- Look under “Conditions” for “Logical grouping” and select “Match Any”
Move down to the “Action” section of the rule
- Under “Action”, chose “Action type” of “Redirect”
- Under “Redirect URL:” enter a URL of choice. I entered http://www.google.com for the URL to redirect to.
- Now click “Apply” on the top far right.
- The rule is built and ready to use.
- Enter the URL http://localhost/phpinfo.php or the second URL for the second file – http://localhost/somefile.html.
What should happen is that you can test one of the two files to redirect to the URL you like.
Again, if the rule did not work, then you need to examine the settings and adjust as needed.
You can also cut and paste this code into the web.config file:
<?xml version=”1.0″ encoding=”UTF-8″?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name=”Rewrite test”>
<match url=”^article/([0-9]+)/([_0-9a-z-]+)” />
<conditions logicalGrouping=”MatchAll” trackAllCaptures=”false” />
<action type=”Rewrite” url=”article.aspx?id={R:1}&title={R:2}” />
</rule>
<rule name=”wordpress” patternSyntax=”Wildcard”>
<match url=”*” />
<conditions logicalGrouping=”MatchAll” trackAllCaptures=”false”>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
</conditions>
<action type=”Rewrite” url=”index.php” />
</rule>
<rule name=”Example of Redirect” enabled=”false” patternSyntax=”ExactMatch” stopProcessing=”true”>
<match url=”phpinfo.php” />
<conditions logicalGrouping=”MatchAll” trackAllCaptures=”false” />
<action type=”Redirect” url=”http://www.google.com” />
</rule>
<rule name=”Example of Redirect with multiple conditions” enabled=”true” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”(.*)” />
<conditions logicalGrouping=”MatchAny” trackAllCaptures=”false”>
<add input=”{SCRIPT_NAME}” pattern=”phpinfo.php” />
<add input=”{SCRIPT_NAME}” pattern=”404.shtml” />
</conditions>
<action type=”Redirect” url=”http://www.bing.com” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
What have we learned?
In this lesson, we have learned how to configure URLRewrite and to test two rules. One with a single redirect based on a simple configuration and another using multiple conditions to be matched to fire the rule.
This is simply an introduction and we’ll cover an example of a Rewrite operation and also other useful operations that any web administrator would find useful in the next article.