apache 服务器 - 如何将 http 重定向到 https

标签 apache .htaccess http ssl https

我已经尝试在我的 .htaccess 文件中使用 mod_rewrite,但这段代码不起作用,它只是给我错误“重定向太多”

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

任何帮助将不胜感激

最佳答案

如果您使用的是 cloudflare 或 AWS 或类似的负载平衡或内部路由请求的东西,您需要检查 %{HTTP:X-Forwarded-Proto} header 而不是%{HTTPS} 变量。

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]

关于apache 服务器 - 如何将 http 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32792566/

相关文章:

.htaccess - 使用 .htaccess 重定向来自外部站点的错误 URL 链接

javascript - 如何使用 Javascript 设置授权 HTTP header

http - http请求和将http请求文本写入80端口上的tcp/ip套接字有什么区别

php - Linux Ubuntu 14.00 - 找不到 Apache2 文件错误 - 文件存在的地方

apache - 重定向 410 url 模式

php - 为什么我的 php 标签会转换成 html 注释?

php - 安装 SSL 证书后 Wordpress 网站无法正常工作

php - Laravel View 在 Linux 服务器上不工作

mysql - XAMPP 服务器 mysql 未启动 "Attempting to start mysql"

php - 使用 .htaccess 重定向注册 URL