gpt4 book ai didi

linux - .htaccess mod_rewrite 删除 .php 扩展名,但保留 URL 路径

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:49 26 4
gpt4 key购买 nike

我目前有可以正常工作的 PHP 脚本,它们被这样调用:www.example.com/user.php/保罗和www.example.com/tag.php/food

我无法正确重写 .htaccess。我正在努力实现这一目标:www.example.com/user/保罗www.example.com/tag/food

到目前为止,我可以让它将/user 重定向到/user.php,但是/paul 丢失了;破坏我的脚本。

我当前的 .htaccess 看起来像这样:

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]

请帮忙。谢谢!保罗

最佳答案

你可能应该在某处使用这样的规则:

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/,然后是第二个记住的东西。

你可能不想在这条规则中使用[L],重定向不是必需的,用户不需要知道你的脚本确实有一个.php 后缀。

希望这是正确的,我现在无法在工作的 Apache 上检查它。

关于linux - .htaccess mod_rewrite 删除 .php 扩展名,但保留 URL 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6271123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com