Search code examples
regexiisurl-rewrite-module

IIS URL Rewrite Module - Regular Expression for URL


I would like to redirect the below old URL:

https://domain1.xyz.com/content/images/1ca9551bc00357f63c608b5d90ca7c4652187487.png

to the new URL:

https://domain2.xyz.com/originalfiles/1ca9551bc00357f63c608b5d90ca7c4652187487.png

My web.config file has below configuration but I cannot get it to work:

<rewrite>
       <rules>
          <rule name="Rewrite Images to CDN" stopProcessing="true">
                <match url="(.*)" />
                <action type="Redirect" url="https://domain2.xyz.com/originalfiles/{C:1}" appendQueryString="true" redirectType="Permanent" />
                <conditions>
                   <add input="{HTTP_HOST}" pattern="domain1.xyz.com" />
                   <add input="{URL}" pattern="content/images/(/*.*)" />
                </conditions>
         </rule>
       </rules>
</rewrite>

Above configuration redirects to this URL which is incorrect:

https://domain2.xyz.com/originalfiles/content/images/1ca9551bc00357f63c608b5d90ca7c4652187487.png

Solution

  • Please try the following rule:

    <rewrite>
           <rules>
              <rule name="Rewrite Images to CDN" enabled="true" stopProcessing="true">
                    <match url="content/images(.*)" />
                    <conditions>
                       <add input="{HTTP_HOST}" pattern="domain1.xyz.com" />
                    </conditions>
                    <action type="Redirect" url="https://domain2.xyz.com/originalfiles{R:1}" />
               </rule>
            </rules>
    </rewrite>