重定向循环,同时使用的.htaccess重定向所有的HTTP请求到https [英] Redirect Loop while redirecting all http requests to https using .htaccess

查看:1404
本文介绍了重定向循环,同时使用的.htaccess重定向所有的HTTP请求到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的.htaccess文件以下规则

I have the following rules on my .htaccess file

# to redirect http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

# to redirect urls with index.php to /
RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

# to redirect non www requests to www url
RewriteCond %{HTTP_HOST} !^www\.example\.com 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

当我试图访问该网站,它变成一个重定向循环。如何解决这个问题,并重定向正确?

When I am trying to access the website, it turns into a Redirect Loop. How to fix this issue and redirect properly?

推荐答案

以防万一有人在使用的背后负载平衡器Apache的基于HTTP> HTTPS重写时重定向循环,这里的解决方案,为我工作。

Just in case somebody have redirect loop when using Apache http->https rewrite behind load balancer, here's solution that worked for me.

使用的的RewriteCond%{} HTTPS关闭作为Apache的背后负载平衡器时,当负载均衡SSL做的东西,我有同样的问题。

I had the same problem when used RewriteCond %{HTTPS} off for Apache behind load balancer, when load balancer does SSL stuff.

如果HTTPS站点的版本不通过Apache配置ModSSL它不设置%{} HTTPS变量设置为开,保持无限重定向。

If https version of the site is not configured via Apache ModSSL it doesn't set %{HTTPS} variable to "on" and keeps redirecting infinitely.

要解决这个问题最简单的办法是针对所有HTTPS流量到另一个Apache的虚拟主机(SSL时由负载均衡处理)这是主要的一个副本,但具有不同的端口(可以说81)。而在.htaccess mod_rewrite的做的一切,是不是81端口:

The simplest solution to fix it is to target all https traffic to another Apache VirtualHost (when SSL is handled by load balancer) that is the copy of main one, but has different port (lets say 81). And in .htaccess do mod_rewrite for everything that is not on port 81:

ReWriteCond %{SERVER_PORT} !^81$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

要做到这一点,第二个方法是从负载平衡器发出的X转发,原标题到Apache,并在重写条件使用它:

The second way to do this is to send X-Forwarded-Proto header from load balancer to Apache and use it in rewrite condition:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

这篇关于重定向循环,同时使用的.htaccess重定向所有的HTTP请求到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆