Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite negate conditions not working

I want to redirect some pages from an old website (oldsite.com) to a new website (newsite.*) according to the following rules:

  • All first-level children (/sv, /no, /da, etc) should redirect to their respective counterparts, i.e. newsite.se, newsite.no, newsite.dk, etc.
  • All other children/descendants should redirect to the root of the new sites as well, except /page1 and /page2 and its descendants.

For this I've created the following rules (for sv in this case):

<rule name="Redirect /sv to .se" stopProcessing="true">
    <match url="^sv/?$" />
        <action type="Redirect" url="http://newsite.se" />
</rule>
<rule name="Redirect /sv/* except some pages" stopProcessing="true">
    <match url="^sv/.+" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_URI}" pattern="^sv/page1(.*)" negate="true" />
        <add input="{REQUEST_URI}" pattern="^sv/page2(.*)" negate="true" />
    </conditions>
    <action type="Redirect" url="http://newsite.se" />
</rule>

The first rule works fine but not the second. The problem is that my negated conditions don't seem to work. When I enter oldsite.com/sv/page1 I still get redirected to newsite.se. Maybe I've misunderstood how negated conditions work, but shouldn't the second rule execute the action if and only if both conditions are true (evaluate to false), i.e. the REQUEST_URI doesn't match /page1 and /page2?

like image 918
William Avatar asked Apr 01 '16 08:04

William


1 Answers

You understand the concept very well.

The only problem is that the {REQUEST_URI} always start with a /, and the url never matches with a pattern like ^sv/page1(.*), finally you have a brand new false positive.

So, you need to include leading slashes in the pattern.

<add input="{REQUEST_URI}" pattern="^/sv/page1(.*)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/sv/page2(.*)" negate="true" />
like image 118
Kul-Tigin Avatar answered Sep 29 '22 13:09

Kul-Tigin