0

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>
  • Mistake 1 in https://blog.lextudio.com/the-very-common-mistakes-when-using-iis-url-rewrite-module-a2ab7e4fee59 – Lex Li Nov 14 '19 at 15:26
  • Updated the rule. Still not working but i think i'm getting close. – Storm-Amber Nov 14 '19 at 16:30
  • Nope. Still wrong pattern value in the condition part. If you want to match `www.example.com`, then it must be strictly `^www.example.com$`. You have to study some regular expression using a search engine. – Lex Li Nov 14 '19 at 16:43
  • Updated again. This should actually be easier ;-) – Storm-Amber Nov 14 '19 at 17:37
  • Actually, with www, it threw an error 500. – Storm-Amber Nov 14 '19 at 19:27
  • That's because you made another mistake by using `https://subdomain.{HTTP_HOST}/{R:1}`. Use hard coded `subdomain.example.com` instead of `subdomain.{HTTP_HOST}/`, and then try again. If you hit another issue, edit your question to include the actual error page. Merely saying "error 500" won't help anyone reading this. – Lex Li Nov 14 '19 at 19:29

1 Answers1

0

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>
Brando Zhang
  • 22,586
  • 6
  • 37
  • 65