删除php扩展名,停止使用.php扩展名访问网址,并删除结尾的斜杠 [英] remove php extension, stop access of url with .php extension and remove trailing slashes
问题描述
我希望自己的网址是无扩展名的,所以没有.php扩展名,我也希望没有机会以斜杠结尾的URL.
I want my urls to be extensionless, so no .php extension, I also want there not to be an opportunity to access the URL with a trailing slash.
以下内容将删除php扩展名,然后如果您尝试使用.php来将其重定向到无扩展名的URL
The following removes php extension and thens redirects to the extensionless url if you try to access it with .php
我开始编写一条规则,以阻止您使用/进行访问并重定向,但它没有用,有帮助吗?
I started writing a rule to stop you accessing with a / and redirect, but it does not work, any help?
#this removes php extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
# stops you accessing url with / **DOESNT WORK**
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [R=301,L]
推荐答案
您正在向后看:您拥有的第一条规则不是删除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
.
这篇关于删除php扩展名,停止使用.php扩展名访问网址,并删除结尾的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!