且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除php扩展名,停止使用.php扩展名访问网址,并删除结尾的斜杠

更新时间:2022-04-16 01:08:23

您正在向后看:您拥有的第一条规则不是删除php扩展名",而是添加了它到尚不存在的网址(从技术上讲,不包含句点的网址).

You're looking at things backwards: the first rule you have doesn't "remove the php extension", it adds it to URLs that don't already have it (technically, any that don't contain a period).

我认为您想要更多类似这样的东西:

I think you want something more like this:

RewriteEngine On
RewriteBase /

# Remove .php from any URLs that contain it, using an external 301 redirect
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)\.php$  $1  [NS,R=301,L]

# Now add it back internally
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)$  $1.php?no-redirect-loop  [NS,QSA]

编辑:调试时

While debugging another similar answer, I realized that the previous solution I posted here wasn't going to work in an .htaccess file. I've edited the example code above to use a rather ugly kluge for breaking redirect loops instead. A side effect of the kluge is that all scripts will see an extra empty URL parameter named no-redirect-loop.