开发者

Exclude directory in URL Rewrite

I have this code at my web.config

<rule name="Imported Rule 2" stopProcessing="tr开发者_StackOverflow中文版ue">
  <match url="^(.*)$" ignoreCase="false" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
  </conditions>
  <action type="Rewrite" url="default.asp?q={R:1}" appendQueryString="true" />
</rule>

and I want that specific directories will exclude of this rule. How can I do it?


To exclude specific folders (/contact/, /presentation/, /db/site/ -- anything in these folders) from being processed by this rule, you can add one more condition, like this:

<rule name="Imported Rule 2" stopProcessing="true">
    <match url="^(.*)$" ignoreCase="false" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(contact|presentation|db/site)" negate="true" />
    </conditions>
    <action type="Rewrite" url="default.asp?q={R:1}" appendQueryString="true" />
</rule>

It is good to do via additional condition because it is easy to read/understand what this rule is about.


If you are good with regex in general, then you may prefer this approach: move such condition into match pattern (you will have the same result in the end and it will be tiny bit faster .. but a bit more difficult to read):

<rule name="Imported Rule 2" stopProcessing="true">
    <match url="^(?!(?:contact|presentation|db/site)/)(.*)$" ignoreCase="false" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="default.asp?q={R:1}" appendQueryString="true" />
</rule>


This is how I got my blog directory to work with code ignitor in root and wordpress in /blog/

The blog folder also has the original web.config file which is just the second rule of this file...

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <rewrite>
        <rules>        
            <rule name="wordpress - Rule 1" stopProcessing="true">
                <match url="^blog" ignoreCase="false"/>
                <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                </conditions>
                <action type="Rewrite" url="/blog/index.php"/>
            </rule>
            <rule name="app" patternSyntax="Wildcard">
                <match url="*"/>
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                </conditions>
                <action type="Rewrite" url="index.php"/>
            </rule>
    </rules>
</rewrite>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜