IIS - 提供无扩展名 PHP 文件,带或不带尾部斜杠
IIS - Serve extensionless PHP files with or without a trailing slash
我不是很熟悉 IIS,但我试图从请求中隐藏 .php
扩展,我也希望它在添加 尾部斜线时工作 .
下面的代码可以很好地处理没有 .php 扩展名的 PHP 文件,但它不适用于尾部斜杠 (404)。
<rule name="rewrite php">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
允许它使用或不使用尾部斜杠的正确方法是什么?
我的解决方案如下:
<rule name="Add Trailing Slash" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}/" />
</rule>
<rule name="rewrite php">
<match url="(.*)(/)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
第一部分强制所有非目录和非直接文件请求的尾部斜杠。注意第二个块中的 <match url="(.*)(/)" />
。这会将尾部斜线分隔成一个单独的正则表达式匹配组。这样,当 使用 PHP 扩展名 (<action type="Rewrite" url="{R:1}.php" />
) 请求文件时,将从 URI 中删除尾部斜杠。
我不是很熟悉 IIS,但我试图从请求中隐藏 .php
扩展,我也希望它在添加 尾部斜线时工作 .
下面的代码可以很好地处理没有 .php 扩展名的 PHP 文件,但它不适用于尾部斜杠 (404)。
<rule name="rewrite php">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
允许它使用或不使用尾部斜杠的正确方法是什么?
我的解决方案如下:
<rule name="Add Trailing Slash" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}/" />
</rule>
<rule name="rewrite php">
<match url="(.*)(/)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
第一部分强制所有非目录和非直接文件请求的尾部斜杠。注意第二个块中的 <match url="(.*)(/)" />
。这会将尾部斜线分隔成一个单独的正则表达式匹配组。这样,当 使用 PHP 扩展名 (<action type="Rewrite" url="{R:1}.php" />
) 请求文件时,将从 URI 中删除尾部斜杠。