zf2 - 使用.htaccess强制SSL/https

3
我已经遵循了这个问题和这个问题,但并没有对我有太大的帮助。
我使用的是Zend Framework2。
我收到了“页面重定向错误”的提示。
以下是我的.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
@brutuscat 我不知道那个。请问如何检查,帮帮忙.. - Keyur
5个回答

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>

一个好的答案:最佳实践:将HTTP重定向到HTTPS(标准域名)

我已经测试过你的解决方案。完美运行。 - Sébastien Gicquel

0
你试过这个吗?
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

它没有强制 SSL。(已通过清除浏览器缓存/cookie 进行测试。) - Keyur

0
你可以试试这个。
#Rewrite to HTTPS
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这对我有用。


0

尝试在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();
    }
}

尝试了两种方法,仍然出现相同的错误。页面无法正确重定向。 - Keyur
尝试将项目环境更改为APP_URL=http://example.com的https。 - Aniket Singh
我们在任何配置文件中都没有声明过这种类型的“APP_URL”。 - Keyur

0

为了强制所有网页流量使用HTTPS,请在您网站根目录下的.htaccess文件中插入以下代码:

重要提示:如果您的.htaccess文件中已经存在其他规则,请将此代码添加到具有类似起始前缀的规则之前。

为了强制特定域名使用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的实际域名和文件夹。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接