.htaccess强制HTTPS [英] .htaccess force HTTPS

查看:73
本文介绍了.htaccess强制HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力寻找一种使用.htaccess强制在我的网站上使用HTTPS的方法。我尝试过的任何方法都会导致重定向循环。我找到了一个PHP替代方案,并对其进行了修改以适合我的Web服务器,但是如果有人可以帮助,我希望使用.htaccess版本。

I've been struggling for a while to find a method of using .htaccess to force HTTPS on my website. Any way I've tried has resulted in a redirect loop. I found a PHP alternative and amended it to suit my webserver, but I'd like the .htaccess equivalent if anybody could help.

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] != "https") {
    header("HTTP/1.1 301 Moved Permanently");
    $location = "https://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
    header("Location: $location");
    exit;
}


推荐答案

您可以在您的根.htaccess:

You can use this rule in your root .htaccess:

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE]

这篇关于.htaccess强制HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆