简体   繁体   English

zf2 - 使用.htaccess强制SSL / https

[英]zf2 - Force SSL/https using .htaccess

I have followed this question and also this question But didn't helped me much. 我已经关注了这个问题以及这个问题,但对我没有多大帮助。

I'm on Zend Framework2 我在Zend Framework2上

I'm getting page is not redirecting properly error. 我正在获取page is not redirecting properly错误。

This is my .htaccess file, 这是我的.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>

I have added this at the starting of the .htaccess file, 我在.htaccess文件的开头添加了这个,

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

But didn't worked for me. 但是对我没有用。

What am I doing wrong? 我究竟做错了什么?

Edit - Apache version is 2.2.15. 编辑 - Apache版本是2.2.15。

Favori solution. Favori解决方案。

<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>

A nice answer Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain) 一个很好的答案最佳实践:301将HTTP重定向到HTTPS(标准域)

Try using in htaccess 尝试在htaccess中使用

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

or just add an function to your bootstrap file 或者只是在引导文件中添加一个函数

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

Have you tried this? 你试过这个吗?

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

You can try this 你可以试试这个

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

This worked for me. 这对我有用。

To force all web traffic to use HTTPS insert the following lines of code in the .htaccess file in your website's root folder. 要强制所有Web流量使用HTTPS,请在网站根文件夹的.htaccess文件中插入以下代码行。

Important: If you have existing code in your .htacess, add this above where there are already rules with a similar starting prefix. 重要提示:如果您的.htacess中有现有代码,请将上面的代码添加到已有具有类似起始前缀的规则的位置。

To force a specific domain to use HTTPS, use the following lines of code in the .htaccess file in your website's root folder: 要强制特定域使用HTTPS,请在网站根文件夹中的.htaccess文件中使用以下代码行:

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

Make sure to replace example.com with the domain name you're trying force to https. 请务必使用您尝试强制为https的域名替换example.com。 Additionally, you need to replace www.example.com with your actual domain name. 此外,您需要将www.example.com替换为您的实际域名。

If you want to force SSL on a specific folder you can insert the code below into a .htaccess file placed in that specific folder: 如果要在特定文件夹上强制使用SSL,可以将下面的代码插入放在该特定文件夹中的.htaccess文件中:

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

Make sure you change the folder reference to the actual folder name. 确保将文件夹引用更改为实际的文件夹名称。 Then be sure to replace www.example.com/folder with your actual domain name and folder you want to force the SSL on. 然后,请务必将www.example.com/folder替换为您要强制启用SSL的实际域名和文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM