【问题标题】:ZF2 - Always open with https?ZF2 - 总是用 https 打开?
【发布时间】:2023-03-05 16:32:02
【问题描述】:

我关注 .htaccess,但我的 ZF2 网站始终不会转到 https,而是仅保持为 http。

当我以www.example.com or example.com or http://www.example.com or example.com/controller 的身份打开网站时,我总是需要它转到https://www.example.com,根本不会留在http://

为什么以下不起作用?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

【问题讨论】:

  • 您是否检查过该 https 位是否实际被访问?您是否尝试将其移至其他规则之前,以防^.*$ [nc,l] 捕获所有内容?
  • 因为第一行 [nc,l] 匹配 EVERYTHING 并终止进一步处理,这意味着永远不会到达 index.php 行。

标签: php apache .htaccess zend-framework


【解决方案1】:

以下似乎有效。我说的是哪里

如果访问者访问以下站点,则始终将其覆盖为 https://

example.com
www.example.com
http://example.com
http://www.example.com
example.com/bla-bla
www.example.com/bla-bla
http://example.com/bla-bla
http://www.example.com/bla-bla

如果访问者确实有 https:// 则忽略 https 覆盖

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

【讨论】:

  • !=on 是错误的。重写正则表达式 不要使用= 进行相等测试。整个模式是隐含的“相等”。 = 没有特殊含义,因此您实际上是在测试 if(%{HTTPS} != "=on"),这始终是正确的。它永远不会有 =on 的值
猜你喜欢
  • 1970-01-01
  • 2011-09-29
  • 2017-08-22
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 2011-04-13
  • 2015-06-27
相关资源
最近更新 更多