【问题标题】:IIS - Serve extensionless PHP files with or without a trailing slashIIS - 提供带有或不带有斜杠的无扩展名 PHP 文件
【发布时间】:2016-08-19 14:19:16
【问题描述】:

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

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

【问题讨论】:

    标签: php azure iis web-config


    【解决方案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>
    

    第一部分强制所有非目录和非直接文件请求的尾部斜杠。注意第二个块中的&lt;match url="(.*)(/)" /&gt;。这会将尾部斜杠分隔为单独的正则表达式匹配组。这样,当使用 PHP 扩展名 (&lt;action type="Rewrite" url="{R:1}.php" /&gt;) 请求文件时,将从 URI 中删除尾部斜杠。

    【讨论】:

    • 我不得不删除这行&lt;add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" /&gt;。我如何也将 .php 文件重定向到最终的 url 路径?
    • @onebitrocket,就我而言,最终的 url 路径是 .php 文件名,但没有 .php 扩展名。你想做什么?以下内容:&lt;add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).php" /&gt; 只是告诉rewrite php 块直接忽略对.php 文件的请求。
    猜你喜欢
    • 2013-09-12
    • 2013-08-24
    • 2018-05-24
    • 2015-07-07
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    相关资源
    最近更新 更多