This article provides an overview of the URL Rewrite Module and explains the configuration concepts that are used by the module.
The URL Rewrite Module rewrites request URLs to simple, user-friendly, and search-engine friendly addresses that are displayed to users or in Web applications. URL Rewrite uses defined rules to evaluate and then map the request URL to the address defined in the rule before it is processed by an IIS Web server. You can define URL rewriting logic that includes regular expressions and wildcards, and rules can be applied based on the request URL, HTTP headers, and server variables. While the primary purpose of the module is to rewrite request URLs to more friendly URLs, you can also use the module to define rules that perform redirects, send custom responses, or abort requests.
A rewrite rule defines the logic of what to compare or match the request URL with, and what to do if the comparison is successful.
Rewrite rules consists of the following parts:
Rewrite rules can be defined in two different collections:
Each configuration level in IIS can have zero or more rewrite rules defined. The rules are evaluated in the same order in which they are specified. The URL Rewrite Module processes the set of rules by using the following algorithm:
A rule may have the StopProcessing flag turned on. When this flag is turned on, it means that no more subsequent rules will be processed and the URL produced by this rule will be passed to the IIS request pipeline (if the rule matched). By default, this flag is turned off.
If rules are defined on multiple configuration levels, the URL Rewrite Module evaluates the rules in the following order:
The URL Rewrite Module preserves the original requested URL path in the following server variables:
It is important to understand how certain parts of the URL string can be accessed from a rewrite rule.
For an HTTP URL in this form: http(s)://<host>:<port>/<path>?<querystring>
For example, if a request was made for this URL: http://www.mysite.com/content/default.aspx?tabid=2&subtabid=3, and a rewrite rule was defined on the site level then:
Note that the input URL string passed to a distributed rule is always relative to the location of the Web.config file where the rule is defined. For example, if a request is made for http://www.mysite.com/content/default.aspx?tabid=2&subtabid=3, and a rewrite rule is defined in the /content directory, then the rule gets this URL string default.aspx as an input.
A rewrite rule pattern is used to specify a pattern to which the current URL path is compared. Current, in this context, means the value of the URL path when the rule is applied. If there were any rules that preceded the current rule, they may have matched the original requested URL and modified it. The URL string that is evaluated against the pattern does not include the query string. To include the query string in the rule evaluation you can use the QUERY_STRING server variable in the rule’s condition. For more information, refer to ”Using server variables in rewrite rules".
A pattern is specified within a <match> element of a rewrite rule.
Rule pattern syntax can be specified by using the patternSyntax attribute of a rule. This attribute can be set to one of the following options:
ECMAScript – Perl compatible (ECMAScript standard compliant) regular expression syntax. This is a default option for any rule. This is an example of the pattern format: ”^([_0-9a-zA-Z-]+/)?(wp-.*)”
Wildcard – Wildcard syntax used in IIS 7 HTTP redirection module. The following is an example of a pattern in this format: “/Scripts/*_in.???”, where asterisk (“*”) means “match any number of any characters and capture them in a back-reference” and “?” means match exactly one character (no back-reference is created).
The scope of the patternSyntax attribute is per rule, meaning that it applies to the current rule’s pattern and to all patterns used within conditions of that rule.
A pattern can be negated by using the negate attribute of the <match> element. When this attribute is used, the rule action is performed only if the current URL does not match the specified pattern.
By default, case-insensitive pattern matching is used. To enable case sensitivity, you can use the ignoreCase attribute of the <match> element of the rule.
Rule conditions allow defining additional logic for rule evaluation, which can be based on inputs other than just a current URL string. Any rule can have zero or more conditions. Rule conditions are evaluated after the rule pattern match is successful.
Conditions are defined within a <conditions> collection of a rewrite rule. This collection has an attribute called logicalGrouping that controls how conditions are evaluated. If a rule has conditions, then the rule action is performed only if rule pattern is matched and:
A condition is defined by specifying the following properties:
Condition input specifies which item to use as an input for the condition evaluation. Condition input is an arbitrary string that can include server variables and back-references to prior condition patterns and/or to rule patterns.
The match type can be one of the following three options:
In addition, the result of the condition evaluation can be negated by using the negate attribute. This can be used to specify a condition that checks if the requested URL is NOT a file, as in the following example:
<add input=”{REQUEST_FILENAME}” matchType=”isFile” negate=”true”>A rewrite rule action is performed when the current URL matches the rule pattern and the condition evaluation succeeded ( depending on the rule configuration, either all conditions matched or any one or more of the conditions matched). There are several types of actions available, and the typeattribute of the <action> configuration element can be used to specify which action the rule performs. The following sections describe different action types and the configuration options related to specific action types.
A Rewrite action replaces the current URL string with a substitution string. A substitution string must always specify the URL path (for example, contoso/test/default.aspx). Note that substitutions that contain a physical path on a file system (for example, C:\inetpub\wwwroot) are not supported in IIS.
A Rewrite action has the following configuration options:
A Redirect action instructs the URL Rewrite Module to send a redirect response back to the client. The redirect status code (3xx) can be specified as a parameter for this action. The Location field of the response contains the substitution string specified in the rule.
The substitution URL for the redirect rule can be specified in one of the following forms:
Usage of a Redirect action implies that no subsequent rules evaluated for the current URL after redirection is performed.
A Redirect action has the following configuration options:
A CustomResponse action causes the URL Rewrite Module to respond to the HTTP client by using a user-specified status code, subcode, and reason. Use of a CustomResponse action implies that no subsequent rules are evaluated for the current URL after this action is performed.
CustomResponse action has the following configuration options:
An AbortRequest action causes the URL Rewrite Module to drop the HTTP connection for the current request. The action does not have any parameters. Use of this action implies that no subsequent rules are evaluated for the current URL after this action is performed.
A None action is used to specify that no action is performed.
Server variables provide additional information about current HTTP requests. You can sue this information to make rewriting decisions or to compose the rewritten URL. Server variables can be referenced in the following locations within rewrite rules:
Server variables can be referenced by using the {VARIABLE_NAME} syntax. For example, the following condition uses the QUERY_STRING server variable:
<add input=”{QUERY_STRING}” pattern=”id=([0-9]+)” />Server variables can also be used to access HTTP headers from the current request. Any HTTP header supplied by the current request is represented as a server variable that has a name generated in accordance to this naming convention:
For example, in order to access the HTTP header “user-agent” from a rewrite rule, you can use the {HTTP_USER_AGENT} server variable.
Parts of rules or conditions inputs can be captures in back-references. These can be then used to construct substitution URLs within rules actions or to construct input strings for rule conditions.
Back-references are generated in different ways, depending on which kind of pattern syntax is used for the rule. When an ECMAScript pattern syntax is used, a back-reference can be created by putting parenthesis around the part of the pattern that must capture the back-reference. For example, the pattern ([0-9]+)/([a-z]+)\.html will capture 07 and article in back-references from this requested URL: 07/article.html. When “Wildcard” pattern syntax is used, the back-references are always created when an asterisk symbol (*) is used in the pattern. No back-references are created when “?” is used in the pattern. For example the pattern */*.html will capture contoso and test in back-references from this requested URL: contoso/test.html.
Usage of back-references is the same regardless of which pattern syntax was used to capture them. Back-references can be used in the following locations within rewrite rules:
Back-references to condition patterns are identified by {C:N} where N is from 0 to 9. Back-references to rule patterns are identified by {R:N} where N is from 0 to 9. Note that for both types of back-references, {R:0} and {C:0}, will contain the matched string.
For example, in this pattern:
^(www\.)(.*)$For the string: www.foo.com the back-references will be indexed as follows:
{C:0} - www.foo.comWithin a rule action, you can use the back-references to the rule pattern and to the last matched condition of that rule. Within a condition input string, you can use the back-references to the rule pattern and to the previously matched condition.
The following rule example demonstrates how back-references are created and referenced:
<rule name="Rewrite subdomain">The URL Rewrite Module controls the IIS output cache behavior in order to:
The module controls output caching either by altering certain caching properties or by disabling the caching altogether. The module cannot enable output caching if it has been disabled by IIS configuration or by any other module in the IIS pipeline. The output caching is controlled as follows:
1. The module always sets the user mode cache setting varyByHeader=”HTTP_X_ORIGINAL_URL”. This ensures that when user mode caching is enabled the module takes into account the original URL to construct a key for the cache entry.
2. If a rewrite rule set uses server variables with values that are either constant throughout the life of the process or are derived from the requested URL, the rule set is considered safe for output caching. This means that the URL Rewrite Module will not alter existing caching policy in any way other than settingvaryByHeader as described in step 1.
The following server variables, when used in rewrite rules, do not cause any effect on output caching policy:
"CACHE_URL",
"DOCUMENT_ROOT",
"HTTP_URL",
"HTTP_HOST",
"PATH_INFO",
"PATH_TRANSLATED",
"QUERY_STRING",
"REQUEST_FILENAME",
"REQUEST_URI",
"SCRIPT_FILENAME",
"SCRIPT_NAME",
"SCRIPT_TRANSLATED",
"UNENCODED_URL",
"URL",
"URL_PATH_INFO",
"APP_POOL_ID",
"APPL_MD_PATH",
"APPL_PHYSICAL_PATH",
"GATEWAY_INTERFACE",
"SERVER_SOFTWARE",
"SSI_EXEC_DISABLED"
3. If a rewrite rule set uses any server variable not mentioned in the above list, the rule set is considered unsafe for output caching. This means that the URL Rewrite Module will disable kernel mode caching for all requests whether the request URLs were rewritten or not. In addition, the module will alter the caching policy for user-mode cache by setting the caching property varyByValue to contain the concatenated string of all server variables values used in the rule set.
There are three string functions available for changing the values within a rewrite rule action, as well as any conditions:
The functions can be invoked by using the following syntax:
{function_name:any_string}Where "function_name" can be on eof the following: "ToLower", "UrlEncode", "UrlDecode". "Any_string" can be either a literal string or a string built by using server variables or back-references. For example, the following are valid invocations of string functions:
{ToLower:DEFAULT.HTM}The string functions can be used in the following locations within rewrite rules:
An example of a rule that uses the ToLower function:
<rule name="Redirect to canonical url">An example of a rule that uses the UrlEncode function:
<rules>An example of a rule that uses the UrlDecode function:
<rules>A rewrite map is an arbitrary collection of name-value pairs that can be used within rewrite rules to generate the substitution URL during rewriting. Rewrite maps are particularly useful when you have a large set of rewrite rules and all of these rules use static strings (that is, when there is no pattern matching used). In those cases, instead of defining a large set of simple rewrite rules, you can put all the mappings into the rewrite map as keys and values between the input URL and the substitution URL. Then, to look up the substitution URL based on the input URL, you will have one rewrite rule that references the rewrite map.
A rewrite map defines a named collection of name-value pair strings, as in the following example:
<rewriteMap name="MyRewriteMap" defaultValue="">A rewrite map is uniquely identified by its name and can contain zero or more key-value entries. In addition, a rewrite map can specify the default value to use when a key is not found. This is controlled by using the defaultValue attribute. By default, an empty string is used as a default value.
There can be any number of rewrite maps on any configuration level, except the file level. Rewrite maps are located within <rewriteMaps> collection element.
Rewrite maps are referenced within a rewrite rule by using the following syntax:
{RewriteMapName:Key}Where the Key parameter can be any arbitrary string, and can include back-references to rule or condition patterns. For example, the following are valid uses of a rewrite map:
{MyRewriteMap:contoso/{R:1}/test/{C:1}}A reference to a rewrite map gets substituted with the value that was looked up by using the key passed as a parameter within a rewrite map reference. If a key was not found, the default value for that rewrite map is used.
A Rewrite map can be referenced in the following locations within rewrite rules:
Example 1: With a rewrite map defined as follows:
<rewrite>And a rewrite rule defined as follows:
<rewrite>The requested URL /diagnostic will be rewritten as /default.aspx?tabid=2&subtabid=29.
The requested URL /webcasts will be rewritten to /default.aspx?tabid=2&subtabid=24.
The requested URL /php will be rewritten to /default.aspx?tabid=7116.
The requested URL /default.aspx will not be rewritten because the rewrite map does not contain an element with key=”/default.aspx”; therefore the rewrite map will return an empty string which will not match the condition pattern, hence the rule action will not be performed.
Example 2: With a rewrite map defined as follows:
<rewrite>And a rewrite rule defined as follows:
<rewrite>The requested URL /default.aspx?tabid=2&subtabid=29 will be redirected to http://www.contoso.com/diagnostics.
The requested URL /default.aspx?tabid=2&subtabid=24 will be redirected to http://www.contoso.com/webcasts.
The requested URL /default.aspx?tabid=7116 will be redirected to http://www.contoso.com/php.
The requested URL /default.aspx will not be redirected because rewrite map does not contain an element with key=”/default.aspx”; therefore the rewrite map will return an empty string which will not match the condition pattern, hence rule action will not be performed.