【问题标题】:Rewriterule redirects to http by defaultRewriterule 默认重定向到 http
【发布时间】:2016-12-12 08:32:51
【问题描述】:

即使是 https 请求,Apache 似乎也会默认重定向到 http。我们有数以千计的重写规则,例如:

RewriteRule ^/testredirect$ /contact.html [R=301,NC,L,NE]

如果请求是https://www.example.com/testredirect,重定向基本上是这样的:

https://www.example.com/testredirect =>  http://www.example.com/contact => https://www.example.com/contact

由我们默认的 http 到 https 规则引起的第二次重定向。这会导致 2 次重定向,从而影响 SEO。

是否可以通过使用相对 url 在保留请求的协议的情况下进行重定向?或者我们应该通过设置一个完整的 url 来更新我们所有的重定向规则:

RewriteRule ^/testredirect$ https://example.com/contact.html [R=301,NC,L,NE]

相关重写日志:

[Tue Dec 13 13:56:37.992575 2016] [rewrite:trace2] [pid 9268:tid 139924464367360] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a60a0/initial] init rewrite engine with requested uri /testredirect
[Tue Dec 13 13:56:37.992628 2016] [rewrite:trace3] [pid 9268:tid 139924464367360] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a60a0/initial] applying pattern '^/testredirect$' to uri '/testredirect'
[Tue Dec 13 13:56:37.992639 2016] [rewrite:trace2] [pid 9268:tid 139924464367360] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a60a0/initial] rewrite '/testredirect' -> '/contact.html'
[Tue Dec 13 13:56:37.992646 2016] [rewrite:trace2] [pid 9268:tid 139924464367360] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a60a0/initial] explicitly forcing redirect with http://www.example.com/contact.html
[Tue Dec 13 13:56:37.992654 2016] [rewrite:trace1] [pid 9268:tid 139924464367360] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a60a0/initial] redirect to http://www.example.com/contact.html [REDIRECT/301]
[Tue Dec 13 13:56:38.161545 2016] [rewrite:trace2] [pid 9268:tid 139924489545472] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a00a0/initial] init rewrite engine with requested uri /contact.html
[Tue Dec 13 13:56:38.161583 2016] [rewrite:trace3] [pid 9268:tid 139924489545472] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a00a0/initial] applying pattern '^/testredirect$' to uri '/contact.html'
[Tue Dec 13 13:56:38.161592 2016] [rewrite:trace1] [pid 9268:tid 139924489545472] mod_rewrite.c(468): [client xxx.xx.xx.xx:4882] xxx.xx.xx.xx - - [www.example.com/sid#7f42ca487138][rid#7f42c40a00a0/initial] pass through /contact.html

【问题讨论】:

  • Apache 默认不重定向到 http,必须有别的东西(在 .conf 文件或 .htaccess 中)做这个重定向
  • @DusanBajic 在您发表评论后我仔细检查了一遍,但除了指定的规则外没有其他内容。还将相关日志添加到问题中。
  • 这个 Apache 前面有负载均衡器吗?
  • @DusanBajic 是的,apache 前面有一个 ELB
  • 和规则说“443(HTTPS,IAM证书:xxx)转发到80(HTTP)”。你认为这有关系吗?

标签: apache redirect mod-rewrite


【解决方案1】:

看起来 HTTPS 在 ELB 上被终止,到达 Apache 的流量始终是纯 HTTP(这就是为什么从 Apache 发送的第一个重定向是到 http://...,Apache 并不简单地知道初始请求是用 @ 发出的987654322@。一旦第二个请求进入 (http://www.example.com/contact),它将跳过您的 /testredirect 规则并点击您的默认 http->https 重定向。

如果您只想始终重定向到https://,只需按照您的建议更改重写规则:

RewriteRule ^/testredirect$ https://example.com/contact.html [R=301,NC,L,NE]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2013-05-29
    • 2019-04-25
    • 1970-01-01
    相关资源
    最近更新 更多