.htaccess 如何进行多次重定向

.htaccess 如何进行多次重定向,.htaccess,redirect,hyperlink,.htaccess,Redirect,Hyperlink,现在我正在更新我的网站,我想将所有链接改为非扩展链接,如example.com/test.php改为example.com/test。我在.htaccess文件中找到的此解决方案: RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.php RewriteRule ^.+\.php$ /%1 [L,R=301] 但同时,我需要重定向位于我网站较低级别的链接,如example.com/level/test2.php到example.com

现在我正在更新我的网站,我想将所有链接改为非扩展链接,如
example.com/test.php
改为
example.com/test
。我在.htaccess文件中找到的此解决方案:

RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.php

RewriteRule ^.+\.php$ /%1 [L,R=301]
但同时,我需要重定向位于我网站较低级别的链接,如
example.com/level/test2.php
example.com/test2.php

我应该在.htaccess中写些什么来将文件从较低级别重定向到较高级别,同时删除.php扩展名?

您需要分离分组并反向引用第二个分组,而不是全部:

RewriteCond %{THE_REQUEST} ^GET\ /([^/?]*/)*([^/?]+)\.php
RewriteRule ^.+\.php$ /%2 [L,R=301]
基本上,
/([^/?]*/)*[^/?]+)\.php
更改为
([^/?]*/)*([^/?]+)
,然后将规则目标中的
%1
更改为
%2