Question

I am trying to create a rewrite rule that will redirect all .html pages to a single URL excluding a single folder (and all sub folders). I've done much googleing and tried to cobble something together from similar situations.

The original rewrite rules works to redirect all html files but I am trying to put a preceeding rule in to stop processing if a folder match is present.

I started with this which works

<rewrite>
  <rules>
    <rule name="redirect legacy html" enabled="true" stopProcessing="true">
      <match url="(.+).html$" ignoreCase="true" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="^$" ignoreCase="false" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="http://www.example.com" />
    </rule>
  </rules>
</rewrite>

I then added a separate rule hoping the first would stop processing and the redirect would not take place

<rewrite>
  <rules>
    <rule name="ignore redirects in admin" enabled="true" stopProcessing="true">
      <match url="/script|admn/(.+).html$" ignoreCase="true" />
      <action type="None" />
    </rule>
    <rule name="redirect legacy html" enabled="true" stopProcessing="true">
      <match url="(.+).html$" ignoreCase="true" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="^$" ignoreCase="false" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="http://www.example.com" />
    </rule>
  </rules>
</rewrite>

This continued to redirect all html files to www.example.com.

I then went on to try and use a condition to exclude certain directories as follows

<rewrite>
  <rules>
    <rule name="redirect legacy html" enabled="true" stopProcessing="true">
      <match url="(.+).html$" ignoreCase="true" />
      <conditions>
        <add input="{REQUEST_URI}" pattern="/(scripts|admn)/.*" ignoreCase="true" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="http://www.example.com" />
    </rule>
  </rules>
</rewrite>

But this is not working either - the redirect still takes place

I've been using RegExr to test my various regular expressions as best I can but I'm not having much luck. I don't know if it is a RegEx issue or a rewrite issue.

Can anyone help?

Was it helpful?

Solution

I managed to solve it using the following

<rewrite>
  <rules>
    <rule name="redirect legacy html" enabled="true" stopProcessing="true">
      <match url="(.+).html$" ignoreCase="true" />
      <conditions>
        <add input="{QUERY_STRING}" pattern="^$" ignoreCase="false" />
        <add input="{REQUEST_URI}" pattern="^/(scripts|admin)" ignoreCase="true" negate="true" />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="http://www.example.com" />
    </rule>
  </rules>
</rewrite>

This was a combination of what I had already tried. Perhaps it will help someone else in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top