IIS将根文件夹重写为不同的子文件夹

时间:2013-05-08 13:13:45

标签: asp.net iis web-config rewrite

我在应用程序中设置了一个类似的文件夹结构:

  • C:\的Inetpub \ wwwroot的\ CONTOSO \公共
  • C:\的Inetpub \ wwwroot的\ CONTOSO \固定

我想将以下网址映射到这些文件夹结构:

我在服务器上安装了Application Request Routing Version 2。我的思考过程是我可以构建一些重写规则来为我做映射,例如这些......

<rewrite>
    <rules>
        <rule name="Rewrite pub page to aspx" stopProcessing="false">
            <match url="^([a-z0-9/]+)$" ignoreCase="true" />
            <conditions>
                <add input="public\{REQUEST_FILENAME}.aspx" matchType="IsFile" ignoreCase="true" />
            </conditions>
            <action type="Rewrite" url="public/{R:1}.aspx" />
        </rule>
        <rule name="Rewrite sec page to aspx" stopProcessing="false">
            <match url="^([a-z0-9/]+)$" ignoreCase="true" />
            <conditions>
                <add input="secured\{REQUEST_FILENAME}.aspx" matchType="IsFile" ignoreCase="true" />
            </conditions>
            <action type="Rewrite" url="secured/{R:1}.aspx" />
        </rule>
        <rule name="Rewrite 404 page to aspx" stopProcessing="true">
            <match url="^([a-z0-9/]+)$" ignoreCase="true" />
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
            </conditions>
            <action type="Rewrite" url="public/default.aspx" />
        </rule>
    </rules>
</rewrite>
<location path="secured"><system.web><authorization><deny users="?"/></authorization></system.web></location>
<location path="public"><system.web><authorization><allow users="?,*"/></authorization></system.web></location>

在我看来,我告诉条件检查文件是否存在于公共文件夹中,如果是,则会重写该文件。否则它会掉线并查看该文件是否存在于安全文件夹中,如果是,则会重写该文件。否则它会被“捕获其他所有”规则所捕获,只需将其指回默认页面即可。

但是这不符合我的期望......我可以让它总是重写到一个文件夹,但是我无法解决检查文件存在的问题。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我在IIS中进行了跟踪,通过查看这些日志,我发现{REQUEST_FILENAME}是在这种情况下使用的错误变量。这是相关的日志信息:

Input secured\{REQUEST_FILENAME}.aspx 
ExpandedInput secured\c:\inetpub\wwwroot\contoso\myaccount.aspx
MatchType 1 
Pattern
Negate false 
Succeeded false 
MatchType IsFile

所以我查看server variables list documentation并找到APPL_PHYSICAL_PATH变量并将输入更改为:

        <rule name="Rewrite sec page to aspx" stopProcessing="false">
            <match url="^([a-z0-9/]+)$" ignoreCase="true" />
            <conditions>
                <add input="{APPL_PHYSICAL_PATH}secured\{R:1}.aspx" matchType="IsFile" ignoreCase="true" />
            </conditions>
            <action type="Rewrite" url="secured/{R:1}.aspx" />
        </rule>

瞧,开始匹配了。希望这有助于将来的其他人。