.htaccess根据PHP参数和一般情况重定向



关于另一个主题:根据PHP参数重定向我得到了帮助根据一些PHP参数进行重定向,例如,

/forum/viewtopic.php?t=123 redirected to /page1.html
/forum/viewtopic.php?t=345 redirected to /page7.html
/forum/viewtopic.php?t=89 redirected to page3.html

提出的(可行的)解决方案为

RewriteEngine On
RewriteCond %{QUERY_STRING} ^t=123$
RewriteRule ^forum/viewtopic.php$ /page1.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=345$
RewriteRule ^forum/viewtopic.php$ /page7.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=89$
RewriteRule ^forum/viewtopic.php$ /page3.html? [L,R=301]

我想在末尾添加一个"一般"情况:如果没有匹配的URL,并且要求文件夹"/forum",重定向到根目录。例如

/forum redirected to /
/forum/viewtopic.php?t=34598237 redirected to /
/forum/something redirected to /

我该怎么做呢?

只需在末尾添加这一行:

RewriteRule ^forum / [L,R=301]

由于前面的所有条件都有[L]修饰符(指示任何进一步的规则处理),因此只有当前面的任何条件不匹配时,最后一个条件才会生效。

最新更新