gpt4 book ai didi

php - 如何从 http 更改为 https?

转载 作者:可可西里 更新时间:2023-11-01 01:03:32 25 4
gpt4 key购买 nike

谁能告诉我如何从 http 更改为 htpps?我的域名有 SSL。我曾经在根目录中创建文件 .htaccess,但它根本不起作用。

这是.htaccess的代码

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

请帮助,提前致谢。

最佳答案

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

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)

但是如果您在代理(例如负载均衡器)后面,则需要使用它发送的 header 。这是我为此使用的代码:

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]

关于php - 如何从 http 更改为 https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16874349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com