重写器默认重定向到http

时间:2016-12-12 08:32:51

标签: apache redirect mod-rewrite

似乎Apache默认重定向到http,即使它是https请求。我们有成千上万的重写规则,如:

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。

是否可以通过使用相对网址来保留所请求的协议来重定向?或者我们应该通过设置完整的网址来更新所有重定向规则:

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

1 个答案:

答案 0 :(得分:1)

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

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

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