【问题标题】:IIS Rewrite root folder to different subfoldersIIS 将根文件夹重写为不同的子文件夹
【发布时间】:2013-05-02 17:50:42
【问题描述】:

我正在一个应用程序中设置一个文件夹结构,如下所示:

  • c:\inetpub\wwwroot\contoso\public
  • c:\inetpub\wwwroot\contoso\secured

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

我在服务器上安装了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>

在我看来,我是在告诉条件检查文件是否存在于公用文件夹中,如果存在,它将重写该文件。否则它将失败并查看文件是否存在于受保护的文件夹中,如果存在,它将重写该文件。否则它会被“catch all else”规则捕获,然后将其指向默认页面。

但这不符合我的预期......我可以让它始终重写到一个文件夹,但我无法获得触发条件来检查文件是否存在。

有什么建议吗?

【问题讨论】:

    标签: asp.net iis web-config rewrite


    【解决方案1】:

    我在 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>
    

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

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2023-04-07
      • 1970-01-01
      • 2014-01-13
      • 2011-12-20
      相关资源
      最近更新 更多