简体   繁体   English

从网址中删除.php扩展名

[英]Removing .php extension from the URL

I have found and used this .htaccess code that will rewrite my .php URLs into ie .com/privacy/ - even if I remove the trailing slash, it will nicely write it; 我已经找到并使用了该.htaccess代码,该代码会将我的.php URL重写为即.com / privacy /-即使删除了斜杠,它也会很好地编写它。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

but when I navigate to site.com/privacy.php it still displays, and doesn't force removal of the extension nor add the trailing slash. 但是当我导航到site.com/privacy.php时,它仍然显示,并且不强制删除扩展名也不添加尾部斜杠。

Can someone help? 有人可以帮忙吗?

You can use this code in root .htaccess: 您可以在根.htaccess中使用以下代码:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Test this after clearing your browser cache. 清除浏览器缓存后进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM