Tag Archives: IIS7.0 URL rewrite module

301 permanent redirection through IIS7.0 URL Rewrite module

The Microsoft URL Rewrite Module 2.0 for IIS 7 enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find. You can use the URL Rewrite module to perform URL manipulation tasks, some of which include:

  • Define powerful rules to transform complex URLs into simple and consistent Web addresses.
  • Easily replace Web application URLs to produce user and search engine friendly results.
  • Rewrite URLs based on HTTP headers and IIS server variables.
  • Perform redirects, send custom responses, or stop HTTP requests based on the logic expressed in the rewrite rules.
  • Control access to Web site content based on URL segments or request metadata.

If your website is on server windows2008 / IIS7.0 then you can easily create a web.config file with 301 redirection rules to set 301 permanent redirection for your website.

To redirect your website to another one,for example,your website is www.example.com and you want to redirect it to www.jayhuang.com then please add the code below in your web.config file between the tags ‘<system.webServer>’ and ‘</system.webServer>’:

<rewrite>
<rule name=”Canonical Host Name” stopProcessing=”true”>
<match url=”(.*)” />
<conditions>
<add input=”{HTTP_HOST}” negate=”true” pattern=”^www\.example\.com$” />
</conditions>
<action type=”Redirect” url=”https://www.jayhuang.com/{R:1}” redirectType=”Permanent” />
</rule>
</rewrite>

To redirect to address that’s with WWW,like from jayhuang.com to www.jayhuang.com,you’ll use the web.config file like below:

<?xml version=”1.0″ encoding=”UTF-8″?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name=”WWW Redirect” patternSyntax=”ECMAScript” stopProcessing=”true”>
<match url=”(.*)” />
<conditions logicalGrouping=”MatchAny”>
<add input=”{HTTP_HOST}” pattern=”^jayhuang\.com” />
</conditions>
<action type=”Redirect” url=”http://www.{C:0}” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Rule Pattern

A rule pattern is used to specify what the rule input string should be matched to. Rule input differs based on the rule configuration:

  • If rule uses tag filters then the content of the matched tag attributed will be passed as an input for the pattern matching.
  • If rule does not use any tag filters then the entire response content will be passed as an input for the pattern matching.

Pattern is specified within a <match> element of a rewrite rule.

Rule pattern syntax

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-.*)”

WildcardWildcard syntax used in IIS 7.0 HTTP redirection module. This is an example of pattern in this format: “/Scripts/*.js”, where asterisk (“*”) means “match any number of any characters and capture them in a back-reference”. Note that wildcard pattern type cannot be used when rule does not have any tag filters.

ExactMatch – exact string search is performed within the input string.

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.

Rule pattern properties

Pattern can be negated by using the negate attribute of the <match> element. When this attribute is used then the rule action will be performed only if the input string does NOT match the specified pattern.

By default, case insensitive pattern match is used. To enable case sensitivity you can use the ignoreCase attribute of the <match> element of the rule.

Rule conditions

Rule conditions allow defining additional logic for rule evaluation, which can be based on inputs other than just a current input 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 will be performed only if rule pattern is matched and:

  • All conditions were evaluated to true, provided that logicalGrouping=“MatchAll” was used.
  • At least one of the conditions was evaluated to true, provided that logicalGrouping=”MatchAny” was used.

A condition is defined by specifying the following properties:

  • Input string – 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.
  • Pattern –  A pattern to look for in the condition input. A pattern can be specified by using either regular expression syntax or by using wildcard syntax. The type of pattern to use in a condition depends on the value of the patternSyntax flag defined for the rule to which this condition belongs. This condition type has two related attributes that control pattern matching:

Rule action

A rewrite rule action is performed when the input string matches the rule pattern and the condition evaluation has succeeded ( depending on rule configuration, either all conditions matched or any one or more of the conditions matched). There are two types of actions available and the “type” attribute of the <action> configuration element can be used to specify which action the rule has to perform. The following sections describe different action types and the configuration options related to specific action types.