使用 .htaccess 强制 SSL 和 WWW [英] Forcing SSL and WWW using .htaccess

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

问题描述

我想知道 .htaccess 中用于在 URL 中强制 SSL 和 WWW 的这段代码是否正确,因为使用另一个代码我通常会得到重定向循环,例如RewriteCond %{HTTPS} !=on 现在它就像一个魅力(可疑).另外,是否可以写得更好/更简单?

I would like to know if this code in .htaccess for forcing SSL and WWW in URL is correct, because with another codes I usually get redirect loop, e.g. RewriteCond %{HTTPS} !=on and now it works like a charm (suspiciously). Also, is possible to write it better/simplier?

# Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Force to WWW
RewriteCond %{HTTP_HOST} !^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]  

推荐答案

那简单一点.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]

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

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