Search code examples
web-configiis-10windows-server-2019

Redirect url with a hashcode to another with same hashcode in IIS10


I need to redirect a url with a variable hashcode to another url with that same hashcode in IIS10 (Windows Server 2019).

Example:

https://www.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd

Needs to redirect to:

https://subdomain.example.com/hello/sd54effg1g5s11d5111dwds21fds2f1ffd

At the moment i have this as a rule in the web.config:

        <rule name="rulename" stopProcessing="true">

         <match url="^hello/[a-zA-Z0-9]+$" />

         <conditions>
          <add input="{HTTP_HOST}" pattern="^(https:\/\/www\.)example.com$" />
         </conditions>        

        <action type="Redirect" url="https://subdomain.{HTTP_HOST}/{R:1}"  />

       </rule>

Solution

  • Firstly, the {HTTP_HOST} is will not match the https part in the url. So you should use ^www.example.com$ instead of ^(https:\/\/www\.)example.com$.

    Besides, you should use https://subdomain.example.com/{R:1} instead of the https://subdomain.{HTTP_HOST}/{R:1} to achieve your requirement.

    Details ,you could refer to below url rewrite rule:

        <rule name="rulename" stopProcessing="true">
    
         <match url="^hello/[a-zA-Z0-9]+$" />
    
         <conditions>
          <add input="{HTTP_HOST}" pattern="^www.example.com$" />
         </conditions>        
    
        <action type="Redirect" url="https://subdomain.example.com/{R:0}"  />
    
       </rule>