【问题标题】:RewriteCond and RewriteRule to do a permanent redirect to https://www. and keep the query stringRewriteCond 和 RewriteRule 永久重定向到 https://www。并保留查询字符串
【发布时间】:2016-06-11 21:48:14
【问题描述】:

我正在使用如下所示的 mod_rewrite 条件/规则将 url 重定向到 www 等效项并确保 https。

RewriteCond %{HTTP_HOST} ^example.com [NC]  
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]

例如,这会将 example.com/foo/bar 转换为 https://www.example.com/foo/bar 并且工作正常。但是,如果链接包含查询字符串(例如,http://example.com/foo/bar?x=baz&y=qux),则不会附加查询字符串。如何修改上面的 RewriteCond/RewriteRule 以便 http://example.com/foo/bar?x=baz&y=qux 自动转换为 https://www.example.com/foo/bar?x=baz&y=qux 附加了查询字符串?我尝试在重写规则中添加 QSA(查询字符串附加)标志,但没有帮助。

【问题讨论】:

    标签: .htaccess redirect mod-rewrite


    【解决方案1】:

    您需要 QSA 标志(查询字符串附加)

    RewriteCond %{HTTP_HOST} ^example.com [NC]  
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC,QSA]
    

    第二个选项

    RewriteRule ^(.*)$ https://www.example.com/$1?%{QUERY_STRING} [L,R=301,NC]
    

    【讨论】:

    • 我会发布更多您的 htaccess,因为可能存在与它冲突的内容
    • 这里还有一点: RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ example.com/$1 [L,R=301,NC] RewriteCond %{REQUEST_FILENAME} ! -f RewriteRule ^ index.php [L]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2016-02-26
    • 2012-08-26
    • 2015-10-16
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多