.htaccess mod_rewrite删除.php扩展名,但保留URL路径 [英] .htaccess mod_rewrite remove .php extension, but preserve URL path
问题描述
我目前有可以正常运行的PHP脚本,如下所示: www.example.com/user.php/paul 和 www.example.com/tag.php/food
I currently have PHP scripts that work properly, being called like this: www.example.com/user.php/paul and www.example.com/tag.php/food
我无法让.htaccess正确重写.我正在努力实现这一目标: www.example.com/user/paul www.example.com/tag/food
I'm having trouble getting .htaccess to rewrite properly. I'm trying to achieve this: www.example.com/user/paul www.example.com/tag/food
到目前为止,我可以将其重定向到/user到/user.php,但是/paul丢失了;破坏我的脚本.
So far, I can get it to redirect /user to /user.php, but the /paul is lost; breaking my script.
我当前的.htaccess文件如下:
My current .htaccess looks like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /([^.\ ]+\.)+php(\?[^\ ]*)?\ HTTP
RewriteRule ^(.+)\.php$ http://www.example.com/$1 [R=301,L]
RewriteRule ^([^/]+/)*index/?$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L]
RewriteCond $1 !^([^.]+\.)+([a-z0-9]+)$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*[^/])$ /$1.php [L]
请帮助. 谢谢! 保罗
Please help. Thanks! Paul
推荐答案
您可能应该在某处使用这样的规则:
You probably should be using a rule like this somewhere:
RewriteRule ^/([^/]*)/([^/]*)$ /$1.php/$2
这又是英文了.
First comes the beginning of the path ^/
The first component of the path doesn't have / symbols in it [^/]*
We remember it ()
Then comes the slash between the components /
Then the second component without the / symbols [^/]*
Remember it too ()
And the path ends $
将其替换为第一个被记住的东西,然后依次为.php/
和第二个被记住的东西.
Replace it with the first remembered things followed by .php/
followed by the second remembered thing.
您可能不想在此规则中使用[L]
,重定向是不必要的,用户不需要知道您的脚本确实具有.php
后缀.
You probably don't want to use [L]
in this rule, the redirect is not necessary, the user doesn't need to know that your scripts really have a .php
suffix.
希望这是正确的,我现在无法在运行正常的Apache上对其进行检查.
Hope this is correct, I cannot check it on a working Apache right now.
这篇关于.htaccess mod_rewrite删除.php扩展名,但保留URL路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!