【问题标题】:zf2 - Force SSL/https using .htaccesszf2 - 使用 .htaccess 强制 SSL/https
【发布时间】:2017-08-22 11:02:03
【问题描述】:

我关注了这个question 和这个question 但对我帮助不大。

我在 Zend Framework2 上

我收到page is not redirecting properly 错误。

这是我的 .htaccess 文件,

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond $1 !^(index\.php|robots\.txt|(.*).gif|(.*).jpg|(.*).png|(.*).jpeg|(.*).GIF|(.*).JPG|(.*).PNG|(.*).JPEG|upload|(.*).js|(.*).css)
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>

<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
application/javascript \
application/json \
application/rss+xml \
application/vnd.ms-fontobject \
application/x-font-ttf \
application/x-web-app-manifest+json \
application/xhtml+xml \
application/xml \
font/opentype \
image/svg+xml \
image/x-icon \
text/css \
text/html \
text/plain \
text/x-component \
text/xml
</IfModule>
</IfModule>

<IfModule mod_php5.c>
    #Session timeout 90 days - 7776000
    php_value session.cookie_lifetime 7776000
    php_value session.gc_maxlifetime 7776000
</IfModule>

我在 .htaccess 文件的开头添加了这个,

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

但对我没有用。

我做错了什么?

编辑 - Apache 版本是 2.2.15。

【问题讨论】:

  • 您是否在为您执行 tls/ssl 终止的负载均衡器后面?
  • @brutuscat 我不知道。如何检查它请帮忙..

标签: php apache .htaccess ssl mod-rewrite


【解决方案1】:

最喜欢的解决方案。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
</IfModule>

一个很好的答案Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)

【讨论】:

  • 我已经测试了您的解决方案。像魅力一样工作。
【解决方案2】:

要强制所有网络流量使用 HTTPS,请在您网站根文件夹的 .htaccess 文件中插入以下代码行。

重要提示:如果您的 .htacess 中有现有代码,请在上面已经存在具有类似起始前缀的规则的位置添加此代码。

要强制特定域使用 HTTPS,请在您网站根文件夹的 .htaccess 文件中使用以下代码行:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

确保将 example.com 替换为您尝试强制 https 的域名。此外,您需要将 www.example.com 替换为您的实际域名。

如果您想在特定文件夹上强制使用 SSL,您可以将以下代码插入到放置在该特定文件夹中的 .htaccess 文件中:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} folder 
RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R,L]

确保将文件夹引用更改为实际文件夹名称。然后确保将 www.example.com/folder 替换为您要强制启用 SSL 的实际域名和文件夹。

【讨论】:

    【解决方案3】:

    你可以试试这个

    #Rewrite to HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    这对我有用。

    【讨论】:

      【解决方案4】:

      你试过了吗?

      RewriteBase /
      RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
      RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
      

      【讨论】:

      • 它不强制使用 ssl。 (通过清除浏览器缓存/cookie 进行测试。)
      【解决方案5】:

      尝试在 htaccess 中使用

      RewriteCond %{HTTP_HOST} ^example.net$ [NC]
      RewriteRule (.*) https://www.example.net/$1 [R=301,L]
      

      或者只是在你的引导文件中添加一个函数

          protected function _initForceSSL() {
          if($_SERVER['SERVER_PORT'] != '443') {
              header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
              exit();
          }
      }
      

      【讨论】:

      • 两种方式都试过了。仍然得到同样的错误。 page is not redirecting properly
      • 尝试将APP_URL=http://example.com所在项目的环境改成https
      • 我们没有在任何配置文件中声明这种类型的APP_URL
      猜你喜欢
      • 2012-02-02
      • 2011-05-22
      • 2015-07-05
      • 2016-09-25
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      相关资源
      最近更新 更多