.htaccess 使用htaccess删除扩展并重定向

.htaccess 使用htaccess删除扩展并重定向,.htaccess,.htaccess,我有这个 RewriteCond %{THE_REQUEST} \s\/(\w+).php [NC] RewriteRule ^ /%1 [R=301,L] RewriteRule ^(\w+)$ /$1.php [L] 这将删除.php扩展名,如果用户直接在浏览器中键入test.php,将加载test。到目前为止还不错 现在我试图让用户也在浏览器中键入此http://example.com/test.php?page=something重定向和加载http://example.com/tes

我有这个

RewriteCond %{THE_REQUEST} \s\/(\w+).php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteRule ^(\w+)$ /$1.php [L]
这将删除
.php
扩展名,如果用户直接在浏览器中键入
test.php
,将加载
test
。到目前为止还不错


现在我试图让用户也在浏览器中键入此
http://example.com/test.php?page=something
重定向和加载
http://example.com/test/something
。这可能吗?

处理参数需要附加规则:

RewriteEngine On

# handle one parameter redirect rule
RewriteCond %{THE_REQUEST} \s/+(\w+)\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]

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

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(\w+)/([\w-]+)/?$ $1.php?page=$2 [L,QSA,NC]

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

您当前的代码是“奇怪的”。为什么是前两行?因为如果用户直接编写
test.php
将重定向并加载
test
。为什么要这样做?而
test
在内部意味着什么?您只需在下一行中再次将其重写为
test.php
,第一行使用
%{the_REQUEST}
可防止无休止的循环。当然,但您还是首先将其从
test.php
重定向到just
test
,然后在内部再次重写为
test.php
。但不管怎样:-)