简体   繁体   中英

web.config rewrite rule for paths

I am trying to create a rewrite rule in the web.config that will take a url such as http://example.org/MySite and just redirect them back to the root site such as http://example.org .

It should also ignore specific folders such as http://example.org/css or http://example.org/js .

<rewrite>
  <rules>
    <rule name="enquiry" stopProcessing="true">
      <match url="http://example.org/^$" /> <!-- Wrong !! ->
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

These 2 rules should be all you need:

<rewrite>
    <rules>
        <rule name="root rule" stopProcessing="true">
            <match url="^$" />
            <action type="None" />
        </rule>
        <rule name="redirect rule" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_URI}" pattern="^/css" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/js" negate="true" />
          </conditions>
          <action type="Redirect" url="/" />
        </rule>
    </rules>
</rewrite>

The first one makes sure the root is not redirected into a redirect loop, and stops processing further rules that otherwise may apply, and the second one excludes the folders that you want to exclude, just add another line to the conditions if there are more folders that you do not want redirected to the root.

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