简体   繁体   中英

IIS URL rewriting rule not working for entire site

My target is to forward the following URL's to the new site (exact matches is needed since I have other rules with oldsite.com/test.aspx):

  • oldsite.com
  • oldsite.com/
  • www.oldsite.com
  • www.oldsite.com/
  • Only the ones from above (statically) i do not want to forward oldsite.com/test.aspx for example

What I used is:

<rule name="Redirect" stopProcessing="true">
                    <match url="oldsite.com$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{HTTP_HOST}" pattern="oldsite.com" />
                        <add input="{HTTP_HOST}" pattern="www.oldsite.com" />
                        <add input="{HTTP_HOST}" pattern="oldsite.com/" />
                        <add input="{HTTP_HOST}" pattern="www.oldsite.com/" />
                    </conditions>
                    <action type="Redirect" url="http://newsite.com" appendQueryString="false" />
                </rule>

My rule doesn't work and I was wondering why

An HTTP Host header never contains a / , so your {HTTP_HOST} conditions ending with / are redundant.

<match url="oldsite.com$" on the other hand simply means that match only with paths (not URLs) ending with oldsite.com .

eg:

  • http://whatever/something-oldsite.com matches.
  • http://whatever/oldsite.com matches.
  • http://whatever/oldsite.comA not matches.
  • http://whatever/oldsite.co not matches.

Apparently this is not what you want.

Besides, bear in mind that a literal dot character ( . ) means "match with any character" in Regular Expressions. This unexpectedly causes your pattern to match match with an URL like http://whatever/oldsiteZcom for example. Remember to escape dots with backslashes in the patterns when you are looking for literal dots.

The url attribute of <match> element is here a little bit ambiguous, I always wonder why Microsoft choose this name, something like <match path=".. would be clearer but this is what we have with the URL Rewrite module. I understand your confusion, I was there.

Anyways, you're looking for exact empty paths so you need a rule like below.

<rule name="Redirect" stopProcessing="true">
    <match url="^$" ignoreCase="false" /> <!-- path is empty, just starts (^) and ends ($), does not contain anything -->
    <conditions>
        <add input="{HTTP_HOST}" pattern="^oldsite\.com$" /> <!-- host name literally equals to oldsite.com -->
        <add input="{HTTP_HOST}" pattern="^www\.oldsite\.com$" /> <!-- host name literally equals to www.oldsite.com -->
    </conditions>
    <action type="Redirect" url="http://newsite.com" appendQueryString="false" />
</rule>

Hope it helps.

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