繁体   English   中英

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

[英]IIS Rewrite root folder to different subfolders

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

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

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

我在服务器上安装了应用程序请求路由版本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>

在我看来,我告诉条件是要检查文件是否存在于公用文件夹中,如果存在,它将重写该文件。 否则,它将失败,并查看该文件是否存在于安全文件夹中,如果存在,它将重写该文件。 否则,它将被“其他所有内容”规则捕获,并将其指向默认页面即可。

但这并没有达到我的期望……我可以始终将其重写为文件夹,但是无法获得触发条件来检查文件是否存在。

有什么建议么?

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

因此,我仔细研究了服务器变量列表文档 ,并能够找到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>

瞧,这开始匹配了。 希望这对以后的人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM