【问题标题】:How to rewrite both .php and .html files in IIS using url rewriting?如何使用 url 重写在 IIS 中同时重写 .php 和 .html 文件?
【发布时间】:2016-07-28 07:10:09
【问题描述】:

我正在使用具有.php 和.html 扩展文件的服务器,我想隐藏它们的扩展名(.php 和.html 文件)。

例如:www.xyz.com/abc.php 到 www.xyz.com/abc 和www.xyz.com/gif.html 到www.xyz.com/gif

我使用的是 IIS 服务器,所以我使用的是 IIS 的 URL 重写模块。

【问题讨论】:

    标签: php html iis url-rewriting web-config


    【解决方案1】:

    你需要在重写之前检查文件是否存在,然后它会继续下一条规则。 像这样的...

    <rule name="rule 1W" stopProcessing="true">
        <match url="^([^\.]+)$"  ignoreCase="true" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{DOCUMENT_ROOT}/{R:1}.php" matchType="IsFile" />
        </conditions>
        <action type="Rewrite" url="/{R:1}.php"  />
    </rule>
    <rule name="rule 2W" stopProcessing="true">
        <match url="^([^\.]+)$"  ignoreCase="true" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{DOCUMENT_ROOT}/{R:1}.html" matchType="IsFile" />
        </conditions>
        <action type="Rewrite" url="/{R:1}.html"  />
    </rule>
    

    【讨论】:

      【解决方案2】:

      将以下代码添加到您的 web.config 文件中

      <system.webServer>
      <rewrite>
      <rules>
      <rule name="rule 1W" stopProcessing="true">
      <match url="^([^\.]+)$"  ignoreCase="true" />
      <action type="Rewrite" url="/{R:1}.php"  />
      </rule>
      </rules>
      </rewrite>
      

      【讨论】:

      • 这仅适用于 .php 文件而不适用于 .html 文件,我需要 类似的东西,如果文件是任何扩展名,它就可以工作
      • 你必须为 .html 添加另一个规则,例如
      • 是的,我尝试了同样的事情,但规则冲突,因此只有第一个规则有效,第二个无效,建议我任何通用操作 url,这样它会寻找 .php,如果没有发现 wud 寻找 .html 文件
      • rule>
      猜你喜欢
      • 2010-11-04
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2011-04-20
      • 2015-07-13
      • 2016-10-30
      • 2014-04-05
      • 1970-01-01
      相关资源
      最近更新 更多