【问题标题】:.htaccess directive exception for html to php in subfolder子文件夹中 html 到 php 的 .htaccess 指令异常
【发布时间】:2018-03-10 12:53:31
【问题描述】:

我有一个网站,我将所有带有 .html 的页面重定向到“same name.php”。

RedirectMatch (.*)\.html$ $1.php

但是我有一个带有 index.html 的子文件夹并且没有使用 php,因此 Apache 请求 index.php 导致 404 错误。我通过制作一个虚拟 index.php 读取 html 文件并将 html 吐出到浏览器来使用一种解决方法。 我尝试了一种“简洁”的方法,在 index.html 的文件夹中使用了一个新的 .htaccess:

RedirectMatch (.*)\.html$ $1.html

但这会导致我已经害怕的循环重定向“重定向过多”。

有没有办法让 index.html 不在此特定文件夹中重定向的文件夹例外?

顺便说一句,我使用 Apache 2.4.x。它发生在 macOS、Ubuntu 和 Android (KSWEB) 上。

【问题讨论】:

    标签: php html .htaccess redirect


    【解决方案1】:

    我尝试了一种“简洁”的方法,在 index.html 的文件夹中使用了一个新的 .htaccess:

    RedirectMatch (.*)\.html$ $1.html
    

    您的RedirectMatch 显然会导致RedirectLoop,因为模式和目标都是相同的。您的 RedirectMatch/file.html 重定向到自身,这就是发生循环错误的原因。

    如果您不想将index.html 重定向到/index.php,您可以使用基于look ahead 的正则表达式模式,该模式匹配除index.html 之外的所有html 文件。

    RedirectMatch ((?!index).+)\.html$ $1.php
    

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2011-12-19
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多