IIS Mod-Rewrite documentation
Overview Documentation    Quick overview    Rewrite directives       RewriteEngine       RewriteRule       RewriteCond       RewriteMap       RewriteOptions       RewriteLog       RewriteLogLevel       RewriteBase       RewriteLock    Support directives    Extended directives    Control center    Apache compatibility Download Purchase FAQ  
IIS Mod-Rewrite - URL rewrite tool for IIS

RewriteCond Directive

A RewriteCond directive, is always bound to the next RewriteRule directive that appears in the configuration script. It's task is to check if extra conditions are met when the bound RewriteRule is matched. If the extra conditions are met then RewriteRule can proceed with URL substitution. If the extra conditions are not met, then RewriteRule does not proceed with URL substitution.

Configuration context
  • per-server configurations
  • per-virtual-host configurations
  • per-directory configurations
  • override (htaccess) configurations
Syntax

RewriteCond TestString Pattern [options]

It requires two arguments. TestString is a text string which is tested against a regular expression defined by Pattern. Optionally, RewriteCond accepts options in a [O1,O2,..,On] format.

One or more consequent RewriteCond directives can precede a RewriteRule. Given a set of directives that consists of one rule and a number of conditions, the execution path is the following:

  1. The input URL is tested against the RewriteRule's regular expression pattern.
  2. If the input URL matches the regular expression above, then the TestString of the first RewriteCond is tested against the same RewriteCond's pattern.
  3. If the condition is met (the TestString matches the Pattern), the next RewriteCond is tested and so on until all RewriteCond directives are tested.
  4. If all the conditions are met, the RewriteRule performs the URL substitution.

TestString expansion

The use of static text as a TestString is very restrictive. To make RewriteCond directive more functional, there are four ways to expand dynamic text within TestString:

Type Symbol Description
RewriteRule backreferences $N, where 0 < N < 9 A RewriteRule backreference is the matched text within a grouping '(...)' of the corresponding RewriteRule's pattern. $0 is the text match a whole, $1 is the first grouped match, $2 the second grouped match and so on.
RewriteCond backreferences %N, where 0 < N < 9 A RewriteCond backreference is the matched text within a grouping '(...)' of the last matched RewriteCond's pattern. %0 is the text match a whole, %1 is the first grouped match, %2 the second grouped match and so on.
Server variables %{VARIABLE_NAME} This returns the value of the requested variable name.
Map calls ${mapname:key|default} The map call returns the value that corresponds to the key, as defined in the RewriteMap named map_name. If the value is not found, the default value is returned.


Apply regular expression

A regular expression is a text matching pattern. In RewriteCond directive, the expanded TestString is used as input for the matching pattern. The table below shows the semantics of the symbols used in a perl compatible regular expression:

Text
Symbol Description Example
. Any single character Every single character, such as a, or z, or 8, or @ etc, is matched.
[] Character class. Any single character within the square brackets. If [abc] is defined, every single character out of a, b or c is matched.
[^] Negated character class. Except every single character within the square brackets. If [^abc] is defined, every single character except from a, b or c is matched.
| Alternative. Right bound OR left bound text. If text1|text2 is defined, then both text1 and text2 are matched.
Quantifiers
Symbol Description Example
? 0 or 1 occurrences of the left bound character or grouped text. If times? is defined, then both time and times are matched.
* 0, 1 or more occurrences of the left bound character or grouped text. If 10* is defined, then all of 1, 10, 100 etc are matched.
+ 1 or more occurrences of the left bound character or grouped text. If 1+ is defined, then all of 1, 11, 111 etc are matched.
Grouping
Symbol Description Example
() Grouped text. The text or the regular expression contained in the parentheses, as a whole, is treated as the subject for matching. Also matches on grouped text are used as backreferences for substitution. If (ab?)+ is defined, then all of a, aa, aaa etc, and ab, abab, ababab etc are matched.
Anchors
Symbol Description Example
^ Start anchor. The right bound character or grouped text must occur at the beginning of the text. If ^abc.* is defined, then abcdef... is matched, but 0123abcdef... is not matched.
$ End anchor. The left bound character or grouped text must occur at the end of the text. If .*abc$ is defined, then ...abc is matched, but ...abcdef is not matched.
Escaping
Symbol Description Example
\ Escape the right bound character. If you want to match a special character, such as $, since this is normally used as the End Anchor symbol, you have to escape it (\$) to be counted as the simple dollar character.


You can also use the character '!' as the NOT symbol at the beginning of the regular expression. This is used in case you want the condition to be met when TestString does NOT match the regular expression.

Alternatively, instead of using a pure regular expression, you can prefix Pattern with one of the following operators:

Operator Description
<Pattern TestString lexicographically precedes Pattern.
>Pattern TestString lexicographically follows Pattern.
=Pattern TestString lexicographically equals Pattern.


In case an operator is used, Pattern must be a static text string. It must not contain expandable references.

Also, a regular expression can be replaced by one of the following flags.

Flag Description
-d TestString is a directory.
-f TestString is a regular existing file.
-s TestString is a regular existing file with size.
-l TestString is a symbolic link.
-x TestString is an existing file with executable permissions.


Options

There are several options that can affect the behavior of a RewriteCond directive.

nocase|NC
Perform case insensitive text matching. TestString will be tested for matching on Pattern ignoring the case of TestString.
ornext|OR
Perform a logical OR with next condition. Normally, a logical AND is preformed between consequent conditions. If a condition that uses this option is met, the next condition (and all the next consequent conditions with the same OR option) is not used.
 
 
Download and try
IIS Mod-Rewrite
NOW!


IIS rewrite

Download IIS Mod-Rewrite

Purchase IIS Mod-Rewrite