简体   繁体   中英

IIS redirect from subdirectory to root

I am trying to redirect requests from the subdirectory of a website to its root. I know there is a rewrite rule option available in IIS but I do not know how to match the URL and then rewrite it. Another issue is that the website has 2 bindings, using 2 different host names, one of which is identical to the subdirectory. I need both of them to redirect requests made to the subdirectory to the root of the website.

Here is a list of possible inputs and their respective desired outputs:

http://host1/host2/abc --> http://host1/abc
http://host1.domain.com/host2/abc --> http://host1.domain.com/abc
http://host2/host2/abc --> http://host2/abc
http://host2.domain.com/host2/abc --> http://host2.domain.com/abc
http://anywebsitename/abc/host2/abc --> http://anywebsitename/abc/host2/abc

Notes:

  • 'abc' can be any string (including empty string).
  • 'host2' is a literal constant.
  • 'host2' subdirectory is a child of the root website.

It's this simple, but you need to apply this at the Host1 (URL Rewrite) in IIS.

<rule name="Rule to point host2-abc to host1-abc" stopProcessing="true">
    <match url="host2/abc" />
    <conditions logicalGrouping="MatchAll" rackAllCaptures="false" />
    <action type="Redirect" url="http://host1/abc" />
</rule>

When this rule gets hit, (you're already in the Host1 area of IIS) so then just look for host2/abc and redirect that back to the parent. This same rule can be applied for all your examples. Please mark this answer as correct, if it solves you issue. Thanks!

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