【问题标题】:How to .htaccess redirect this URL with a query string?如何 .htaccess 使用查询字符串重定向此 URL?
【发布时间】:2011-05-01 17:51:10
【问题描述】:

在使用 WordPress 维护插件时,我最终得到了这个查询字符串 URL,我现在需要在网站上线时将其重定向到 root。

http://mydomain.com/maintenance/?req=http%3A%2F%mydomain.com%2F

什么会将该 URL 重定向到简单的 http://mydomain.com

这两个我都试过了:

Redirect 301 "http://mydomain.com/maintenance/?req=http://mydomain.com/" http://mydomain.com

Redirect 301 "http://mydomain.com/maintenance/?req=http%3A%2F%mydomain.com%2f" http://mydomain.com

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    将这些行放入您的 .htaccess 文件中:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} req=http%3A%2F%2F(.+)%2F [OR] 
    RewriteCond %{QUERY_STRING} req=http://([^&]+)(&|$) [NC]
    RewriteRule ^maintenance/?$ http://%1? [L,R,NC,NE]
    

    【讨论】:

    • 谢谢,效果很好!如何从原始字符串重定向到 mydomain.com/update/ 之类的页面(显示指向 mydomain.com 的链接),以便我可以建议用户在单击并转到 mydomain.com 后更新书签?跨度>
    • 您的意思是将URI /maintenance/?req=http%3A%2F%2Fmydomain.com%2F 重定向到http://mydomain.com/update ?这可以通过上面规则的最后一行来简单地完成,比如RewriteRule ^maintenance/?$ http://%1/update/? [L,R,NC,NE]
    【解决方案2】:

    Redirect 仅适用于 URL 路径。需要使用the real mod_rewrite查看查询:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} =req=http://example.com/
    RewriteRule ^maintenance/$ /? [L,R=301]
    

    【讨论】:

    • 谢谢,但无法正常工作;它在正确的位置,在 RewriteEngine 之后和 WP 重写块之前。
    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 2022-03-24
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多