简体   繁体   中英

IIS rewrite - redirect path to a different URL, including partial path (C# web config)

I have a multisite setup in Sitecore, where my main site is mysite.com and I have a secondary site of secondsite.org. These both work, but the user can also navigate to secondsite.org by going to the Sitecore path for the second site home node, ie mysite.com/second-site-home. I don't want this to happen, so I added a redirect in the webconfig so that mysite.com/second-site-home will redirect to secondsite.org:

<rule name="homepage path Redirect" stopProcessing="true">
  <match url="^second-site-home/?(.*)$" />
  <action type="Redirect" url="http://secondsite.org" redirectType="Found"/>
  <conditions>
    <add input="{HTTP_HOST}" pattern="^(www\.)?mysite.com$" />
  </conditions>
</rule>

This works fine for the homepage, but I have links on mysite.com that go to pages on secondsite.org, and these links are rendering as mysite.com/second-site-home/about, and when I navigate to this link I get redirected to the secondsite homepage, rather than the actual page I want to go to.

Is is possible to modify the redirect so that in the redirect url, it includes the rest of the path after /second-site-home/ ?

You just need to add a back reference variable to your rule:

<rule name="homepage path Redirect" stopProcessing="true">
  <match url="^second-site-home/?(.*)$" />
  <action type="Redirect" url="http://secondsite.org/{R:1}" redirectType="Found"/>
  <conditions>
    <add input="{HTTP_HOST}" pattern="^(www\.)?mysite.com$" />
  </conditions>
</rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM