【问题标题】:URL Rewriting .php to .htmlURL 将 .php 重写为 .html
【发布时间】:2016-01-30 20:02:58
【问题描述】:

我正在将我的.htaccess 中的网址扩展从.php 转换为.html

RewriteEngine On
RewriteBase /    
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L] 
FallbackResource /index.php

问题是我有一些部分带有“php”这个词:

www.mywebsite.com/phpscripts.php 

当它被转换时:

www.mywebsite.com/htmlscripts.html 

【问题讨论】:

    标签: php html url url-rewriting


    【解决方案1】:
    ^(.*).php
    

    此正则表达式表示:任何内容,包括任何内容,后跟一个任意字符,然后是“php”。例如,它会匹配“blahphp.html”,特别是它会匹配“blahphp”部分,根本不关心扩展名。

    你要找的是这个:

    RewriteRule (.+)\.php$ $1.html [R=301,L]
    RewriteRule (.+)\.html$ $1.php [L] 
    

    (.+)\.php$某个东西(至少一个字符),后跟一个句点,在字符串的末尾。您也可以去掉RewriteCond,它不会在这些规则中添加任何内容。

    还请注意,您应该将所有 HTML 文件更改为链接到 href="...html"。不要仅依靠这些重定向来解决您的问题;重定向每个请求不仅效率低下,而且还会破坏 POST 请求。仅允许将出于任何原因尝试打开旧 URL 的客户端重定向到新的规范 URL。

    【讨论】:

    • 不起作用(重定向循环)RewriteEngine On RewriteBase / RewriteRule (.+)\.php$ $1.html [R=301,L] RewriteRule (.+)\.html$ $1。 php [L] FallbackResource /index.php
    • 我用redirectdetctive分析:它多次重定向到www.mywebsite.com/phpscripts.html,所以第一个问题解决了但是如何处理循环
    • 提高 Apache 调试级别并分析 Apache 日志中的重写过程(如果您可以访问)。
    • 老实说我不知道​​该怎么做,问题可能来自标志?并且不了解内部和外部重定向之间的区别
    猜你喜欢
    • 2017-05-31
    • 2014-10-13
    • 1970-01-01
    • 2023-03-11
    • 2022-08-19
    • 2012-06-13
    • 2012-12-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多