IIS URL Rewrite “Rewrite Module error: Expression contains a repeat expression”

How to fix the URL Rewrite Module error β€œRewrite error: Expression contains a repeat expression” on Windows Server IIS. The other day, I had to migrate a website from a Linux/Apache web server to Windows Server IIS. The website in question had a lot of sub domains, all pointing to folders within the web root using that same name: foobar.example.com would redirect (rewrite) to www.example.com/foobar.

For this to happen we usually use an IIS URL Rewrite Module rule for one sub domain in a web.config file:

<rules>
  <rule name="Own website for sub domain">
    <match url="(.*)" />
    <conditions trackAllCaptures="true">
      <add input="{HTTP_HOST}" pattern="^(foobar)\.example\.com$" />
      <add input="{PATH_INFO}" pattern="^/foobar" negate="true" />
    </conditions>
    <action type="Rewrite" url="/{C:1}/{R:1}" />
  </rule>Code language: HTML, XML (xml)

However, this would mean I had to add and use that same rule some fifty times… Not good!

I simplified the rewrites by using the value of the first condition (HTTP_HOST, or foobar) as input for the second condition PATH_INFO. Now logically you want to substitute ^/foobar with ^/{C:1}, to use the value of the first condition as input for the second condition in your rewrite rule:

<add input="{PATH_INFO}" pattern="^/{C:1}" negate="true" />Code language: HTML, XML (xml)

This produced an IIS URL Rewrite Module error:

HTTP Error 500.52 – URL Rewrite Module Error.
The expression β€œ^/{C:1}” contains a repeat expression (one of β€˜*’, β€˜?’, β€˜+’, β€˜{β€˜ in most contexts) that is not preceded by an expression.

HTTP Error 500.52 – URL Rewrite Module Error.
The expression β€œ^/{C:1}” contains a repeat expression (one of β€˜*’, β€˜?’, β€˜+’, β€˜{β€˜ in most contexts) that is not preceded by an expression.

Fixing the IIS URL Rewrite Module error by escaping braces

After some investigation and testing, I found you can fix this IIS URL Rewrite Module error by escaping braces – { } – in the second input condition:

<add input="{PATH_INFO}" pattern="^/\{C:1\}" negate="true" />Code language: HTML, XML (xml)

The complete rewrite becomes:

<rules>
  <rule name="Own website for sub domain">
    <match url="(.*)" />
    <conditions trackAllCaptures="true">
      <add input="{HTTP_HOST}" pattern="^(foobar)\.example\.com$" />
      <add input="{PATH_INFO}" pattern="^/\{C:1\}" negate="true" />
    </conditions>
    <action type="Rewrite" url="/{C:1}/{R:1}" />
  </rule>Code language: HTML, XML (xml)

To make sure you’re not rewriting www.example.com to www.example.com/www, exclude that sub domain first by using negate:

<add input="{HTTP_HOST}" pattern="^www" negate="true" />Code language: HTML, XML (xml)

Jan Reilink

Hi, my name is Jan. I am not a hacker, coder, developer or guru. I am merely an application manager / systems administrator, doing my daily thing at Embrace - The Human Cloud. In the past I worked for clidn and Vevida. With over 20 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization. I blog at https://www.saotn.org.

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
kiquenet
05/09/2019 13:37

HOW-TO redirec http://hostname1:50000 to https://hostname1:50443 ? port number not same.