通过.htaccess将.php ext替换为根文件,而不是子目录文件


Replacing .php ext with .html through .htaccess for root files only not subdirectoy files

如何通过.htaccess将.php-ext替换为根文件而非子目录文件。

示例:www.test.com/home.php应更改为www.test.com/home.html但www.test.com/admin/home.php应该保持不变。

我只是试了一下:

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

请协助我。

您可以在root.htaccess:中使用这样的代码

RewriteEngine on  
RewriteBase /
RewriteCond %{THE_REQUEST} (.*)'.php  
RewriteRule ^([^/.]+)'.php $1.html [R=301,L]  
RewriteRule ^([^/.]+)'.html $1.php [L,NC]

[^/.]+正则表达式将确保它只在根路径中工作。

将.htaccess文件更改为:

RewriteEngine on  
RewriteBase /
RewriteRule ^([^/]*)'.php$ $1.html [R=301,L]
RewriteRule ^([^/]*)'.html$ $1.php [L]

然后更新到根页面的所有链接以链接到*.html