【问题标题】:.htaccess conditional rewriting .html to .php.htaccess 有条件地将 .html 重写为 .php
【发布时间】:2012-11-05 12:53:49
【问题描述】:

首先要说,对不起,如果我重复发布,但我没有找到解决这个问题的方法。

我正在尝试重写我的网址,以便当我输入 somepage.html 时,它会为我服务 somepage.php,但前提是 somepage.html 不存在。任何人都可以帮忙吗?谢谢

【问题讨论】:

    标签: apache .htaccess mod-rewrite rewrite


    【解决方案1】:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^.]+)\.html$ $1.php [L,R=301]
    

    另见Redirect requests only if the file is not found?

    【讨论】:

    • 对不起,但这对我不起作用...例如: www.myurl.com/something.html 如果文件存在则工作正常,但如果没有文件,它会尝试抓取(并将网址更改为):www.myurl.com/emformar/domains/myurl.com/public_html/something.php
    • 尝试在规则前添加RewriteBase /RewriteBase docs
    【解决方案2】:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([A-Za-z0-9-]+)\.html$ $1.php [L,R=301]
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,这是我的解决方案。

      这将检查是否存在 HTML 文件,如果没有,它将使用 PHP - 如果您已将您的网站迁移到 PHP 并且担心任何指向 HTML 页面的第三方链接,这是理想的选择

      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)\.html$ $1.php [R=301,L
      
      ]
      

      【讨论】:

        猜你喜欢
        • 2017-05-31
        • 2012-12-01
        • 2013-02-11
        • 2014-05-22
        • 1970-01-01
        • 2019-03-31
        • 2015-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多