.htaccess 将 http 重写为 https 导致循环

Bri*_*ood 5 rewrite http https .htaccess apache-2.2

在运行 varnish 的 apache 2.2 服务器上,我试图将 http -> 重定向到整个 Drupal Commons ( http://commons.acquia.com/ ) 站点的https 。

由于 varnish 缓存了一些重定向(我认为是 R=301,但不是 R=302),我首先让重定向在 :8080 工作,而 varnish 不提供该重定向。

下面的 .htaccess 代码成功地将 http://foo.example.com:8080 重定向到 https://foo.example.com。(适用于 Chrome、FF 和 Safari。)

# RewriteBase /

### Standard Drupal7 .htaccess above here  ###

# SSL REWRITES
RewriteCond %{SERVER_PORT} ^8080$
RewriteRule ^(.*)$ https://foo.example.com/$1 [NC,R=302,L]

### Standard Drupal7 .htaccess below here  ###

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

如果我

  1. 把上面的8080改成80
  2. 保存.htaccess
  3. 重启清漆
  4. 清除 Drupal 缓存
  5. 清除浏览器缓存
  6. 访问 http://foo.example.com

(每次编辑 .htaccess 后,我都虔诚地执行了上述操作。)

当我访问 http://foo.example.com 时,我在 Chrome、FF、Safari 中得到一个重定向循环。

为了理解循环,我改变了重定向如下:

# SSL REWRITES
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://foo.example.com/$1/%{SERVER_PORT} [NC,R=302,L]
Run Code Online (Sandbox Code Playgroud)

我在我的位置栏中得到了这个:https://foo.example.com/80/80/80/80/80/80/80/80/80/80/80/80/80/80/80/80 /80/80/80/80/80

当浏览器转发到https url时,应该不是SERVER_PORT=443???

更改为 R=301 会导致相同的循环。

测试 %{HTTPS} 会导致相同的循环。

# SSL REWRITES
#RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://foo.example.com/$1/%{HTTPS} [NC,R=301,L]
Run Code Online (Sandbox Code Playgroud)

现在我在我的位置栏中得到这个:https://foo.example.com/off/off/off/off/off/off/off/off/off/off/off/off/off/off/off/off /关闭/关闭/关闭/关闭/关闭

每个http://drupal.org/node/181233#comment-278142(和http://drupal.org/node/823232#comment-3075288)我也试过:

# SSL REWRITES
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://foo.example.com/$1/%{SERVER_PORT} [NC,R=301,L]
Run Code Online (Sandbox Code Playgroud)

试过和不试!-f 和 -d 的否定。无论哪种方式,条件都不匹配,所以什么也不会发生。我不知道为什么需要这些 -f, -d 测试....

我还尝试删除 RewriteRule 中的正则表达式替换

# SSL REWRITES
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://foo.example.com/ [R=301,L]
Run Code Online (Sandbox Code Playgroud)

这仍然会导致循环。

任何帮助将非常感激。

Bri*_*ood 1

我从服务器管理员那里发现他们在该服务器上使用 Pound 终止了 SSL。这意味着 SSL 可以工作,但 HTTP_SERVER 变量没有反映我期望的值。无论真实情况如何,SERVER_PORT 将始终为 80,并且 HTTPS 将始终关闭。

使用 mod_rewrite 时,转储服务器变量以查看可以使用 RewriteCond 测试哪些内容。请参阅http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html#PHP_Code_access_Apache_Variables

您还可以使用大多数编程语言将 http 重定向到 https。我有 Drupal 的 443session 模块与这个站点一起工作——它使用了这种方法。(我不必告诉模块设置规则,即使它无法检测到 HTTPS 已打开。)

确保您也使用安全会话 cookie。除了 mod_ssl 之外,您还需要它们来防止会话劫持。

对于Drupal来说,这是旧的,但是很好http://drupalscout.com/knowledge-base/drupal-and-ssl-multiple-recipes-possible-solutions-https我认为drupal.org/project/443session是目前最好的模块选项。