【问题标题】:.htaccess redirect according to PHP parameters and general case.htaccess 根据 PHP 参数和一般情况重定向
【发布时间】:2015-08-06 14:45:26
【问题描述】:

关于这个其他主题:.htaccess redirect according to PHP parameters 我得到了根据一些 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 /

我该怎么做?

【问题讨论】:

    标签: php apache .htaccess redirect


    【解决方案1】:

    只需在末尾添加这一行:

    RewriteRule ^forum / [L,R=301]
    

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

    【讨论】:

    • 这条规则似乎重定向了 /forum/something 之类的所有内容(一个子文件夹,一个文件......)。是否可以只重定向 mysite.com/forum/,而不重定向(例如,不重定向 /forum/page1.php、/forum/folder)
    • 自己解决了:RewriteRule ^forum/$ / [L,R=301]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2014-02-08
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多