简体   繁体   English

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

[英]IIS Rewrite root folder to different subfolders

I'm setting up a folder structure inside an application that looks like such: 我正在如下所示的应用程序中设置文件夹结构:

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

I am wanting to map the following URLs to those folder structures: 我想将以下URL映射到那些文件夹结构:

I have the Application Request Routing Version 2 installed on the server. 我在服务器上安装了应用程序请求路由版本2 My thought process was that I could build a few rewrite rules to do the mapping for me such as these ... 我的思维过程是,我可以建立一些重写规则来为我做这些映射,例如...

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

In my mind, I was telling the condition to check if the file exists in the public folder and if so it would rewrite that file. 在我看来,我告诉条件是要检查文件是否存在于公用文件夹中,如果存在,它将重写该文件。 Otherwise it'd fall through and see if the file exists in the secured folder and if so it would rewrite that file. 否则,它将失败,并查看该文件是否存在于安全文件夹中,如果存在,它将重写该文件。 Otherwise it would get caught by the "catch everything else" rule and just point it back to a default page. 否则,它将被“其他所有内容”规则捕获,并将其指向默认页面即可。

But this is not working to my expectations ... I can get it to always rewrite to a folder but I can't get the conditions to fire to check for a file existing. 但这并没有达到我的期望……我可以始终将其重写为文件夹,但是无法获得触发条件来检查文件是否存在。

Any suggestions? 有什么建议么?

I turned tracing on within IIS and by looking through those logs I was able to discover that {REQUEST_FILENAME} was the wrong variable to use in this situation. 我在IIS中打开了跟踪功能,通过查看这些日志,我发现{REQUEST_FILENAME}是在这种情况下使用的错误变量。 Here's the relevant log information: 这是相关的日志信息:

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

So I went looking through the server variables list documentation and was able to find the APPL_PHYSICAL_PATH variable and changed the inputs to this: 因此,我仔细研究了服务器变量列表文档 ,并能够找到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>

And voila, that started matching. 瞧,这开始匹配了。 Hope this helps someone else in the future. 希望这对以后的人有所帮助。

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

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