Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 将PHP重定向到HTML,但将HTML指向PHP_.htaccess - Fatal编程技术网

.htaccess 将PHP重定向到HTML,但将HTML指向PHP

.htaccess 将PHP重定向到HTML,但将HTML指向PHP,.htaccess,.htaccess,考虑以下规则 RewriteRule ^index\.html$ index.php [L] RewriteRule ^index\.php$ index.html [R=301] 我希望.html使用.php文件,但我希望url始终具有.html扩展名 上述规则创建重定向循环。正确的方法是什么?您可以根据实际请求进行检查,也可以完全防止循环 选项1,根据请求检查: RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php RewriteRule

考虑以下规则

RewriteRule ^index\.html$ index.php [L]
RewriteRule ^index\.php$ index.html [R=301]
我希望
.html
使用
.php
文件,但我希望url始终具有
.html
扩展名


上述规则创建重定向循环。正确的方法是什么?

您可以根据实际请求进行检查,也可以完全防止循环

  • 选项1,根据请求检查:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
    RewriteRule ^index\.php$ index.html [R=301]
    
    这样,如果实际请求不是
    index.php
    ,重定向就不会发生

  • 选项2,防止重写引擎完全循环。将其添加到htaccess文件的顶部:

    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule ^ - [L]
    
    这样做的缺点是,您可能有实际想要循环的规则


  • 您可以根据实际请求进行检查,也可以完全防止循环

  • 选项1,根据请求检查:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
    RewriteRule ^index\.php$ index.html [R=301]
    
    这样,如果实际请求不是
    index.php
    ,重定向就不会发生

  • 选项2,防止重写引擎完全循环。将其添加到htaccess文件的顶部:

    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteRule ^ - [L]
    
    这样做的缺点是,您可能有实际想要循环的规则


  • 谢谢,请注意解释一下“^[A-Z]{3,9}”在第一个解决方案中做了什么?@stevether
    %{the_REQUEST}
    var实际上是HTTP请求的第一行,例如
    GET/index.php HTTP/1.1
    ,而
    ^[A-Z]{3,9}
    GET
    部分匹配,这是“方法”长度可以在3到9个字符之间。如果您只关心GET/POST,也可以将其更改为
    (GET | POST)
    。谢谢,请注意解释一下`^[A-Z]{3,9}`在第一个解决方案中做了什么?@stevether the
    %{the_REQUEST}
    var实际上是HTTP请求的第一行,例如
    GET/index.php HTTP/1.1
    ,以及
    ^[A-Z]{3,9}
    匹配
    GET
    部分,该部分是“方法”,长度可以在3到9个字符之间。如果您只关心GET/POST,您也可以将其更改为
    (GET | POST)