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)
HOW-TO redirec http://hostname1:50000 to https://hostname1:50443 ? port number not same.