Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.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
使用mod_Rewrite将.html重写为.php_Php_Html_Mod Rewrite - Fatal编程技术网

使用mod_Rewrite将.html重写为.php

使用mod_Rewrite将.html重写为.php,php,html,mod-rewrite,Php,Html,Mod Rewrite,我有一个重写条件和规则,它是 RewriteCond %{DOCUMENT_ROOT}$0 !-f RewriteRule ^[^*]+$ index.php [L] 我需要用%{DOCUMENT\u ROOT}$0中的.php替换.html 原因是,我正在将我的url重写为.html,但当此文件检查现有文件时,由于%{DOCUMENT\u ROOT}$0查找文件thefile.html,因此失败 我需要它查找文件.php以重写现有同名.php文件的.html扩展,请尝试以下规则: Rewri

我有一个重写条件和规则,它是

RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]
我需要用
%{DOCUMENT\u ROOT}$0
中的
.php
替换
.html

原因是,我正在将我的url重写为
.html
,但当此文件检查现有文件时,由于
%{DOCUMENT\u ROOT}$0
查找文件
thefile.html
,因此失败


我需要它查找
文件.php
以重写现有同名
.php
文件的
.html
扩展,请尝试以下规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php?%{QUERY_STRING} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]
您刚刚将任何不存在的文件重写为index.php已解决

RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^(.*).html$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^[^*]+$ index.php [L]

我不得不使用它两次,首先,将所有的.html重新连接到.php,然后在请求.php时,第二个条件开始发挥作用。

谢谢,但是,如果文件不存在,我会收到404错误代码。我需要它转到文件,如果它以.php的形式存在(但请求将是.html),如果没有得到index.phpI,则在末尾添加了两行,因此它应该另外将不存在的文件重定向到index.php,以供那些寻求解释的人使用,第二条重写规则之所以有效,是因为在查找新的PHP文件时,第二次调用.htaccess。请记住,这不会将HTML解释为PHP。这似乎是人们认为这是唯一的真正原因。