Redirect web traffic in web.config

51 Views Asked by At

I would like to redirect all pages from the website from abc.com/staging/ to the root folder of another domain xyz.com/

For example abc.com/staging/about.html should be redirected to xyz.com/about.html

I tried this, but it doesn't work:

<rule name="testredirect" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^abc/staging" />
</conditions>
<action type="Redirect" url="xyz.com/" redirectType="Permanent" />
</rule>
1

There are 1 best solutions below

0
dana On

I think you need to check both {HTTP_HOST} and {PATH_INFO}. The somewhat tricky thing you are trying to do here is remove /staging from the path. I tried using a "capture" {C:1} to handle this, but it is untested.

<rule name="Redirect 'abc.com/staging' to 'example.com'" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^abc\.com$" />
    <add input="{PATH_INFO}" pattern="^/staging/(.*)$" ignoreCase="true" />
  </conditions>
  <action type="Redirect" url="https://example.com/{C:1}" appendQueryString="true" />
</rule>