强制所有页面都为HTTPS

时间:2013-11-01 08:36:16

标签: php apache heroku

我正在使用带有SSL的Heroku php。一切都设置好了,https完美无缺。 我的域名在Godaddy中托管。 Heroku作为apache web服务器。 但http://mysite.com,www.mysite.com不会重定向到https://www.mysite.com 这就是我想要的。

我已将.htaccess文件更改为以下

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} protected [NC,OR]
RewriteCond %{REQUEST_URI} protected2 [NC,OR]
RewriteCond %{REQUEST_URI} protected3 [NC]

我还将域转发到https://www.mysite.com

2 个答案:

答案 0 :(得分:0)

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

Source

答案 1 :(得分:0)

使用htaccess文件:

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

使用PHP指导:

  exit(header("location: https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"));

两者都应该有用。 : - )