IIS - 提供带有或不带斜杠的无扩展PHP文件

时间:2016-08-19 14:19:16

标签: php azure iis web-config

我不是很熟悉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>

允许使用或不使用尾部斜杠的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

我的解决方案如下:

<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中删除尾部斜杠。