3

Does anyone can tell me how to change from http to htpps? My domain name have SSL. I used to create file .htaccess in the root, but it is not work at all.

Here is the code of .htaccess

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://mysite.com/$1 [R=301,L]

Please help, Thank in advance.

4

2 回答 2

3

对于直接访问,这似乎是要走的路:

RewriteEngine on

# rewrite to HTTPS
RewriteCond ${HTTPS} !on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

(基于文档:http ://httpd.apache.org/docs/current/mod/mod_rewrite.html )

但是,如果您使用代理(例如负载均衡器),则需要使用它发送的标头。这是我用于此的代码:

RewriteEngine on

# rewrite to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

它对我很有帮助,而且不言自明。

当然,您可以将两者结合起来使其更健壮,但在您知道如何使用它的任何实际情况下,这都是矫枉过正的。

RewriteEngine on

# rewrite to HTTPS
RewriteCond ${HTTPS} !on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
于 2013-06-01T15:37:57.810 回答
1

根据这个网站(谷歌的第一个结果)

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

应该管用。

于 2013-06-01T15:37:49.473 回答