.htaccess将裸http和https都重定向到www https [英] .htaccess redirect both naked http and https to www https

查看:112
本文介绍了.htaccess将裸http和https都重定向到www https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个域中也遇到类似的问题.我同时为裸域和www域安装了SSL证书.

I have similar issue on one of my domains. I have SSL cert installed both for naked and www-domain.

现在,但是我想将裸 http://domain.com https://www .domain.com .

Now, however I want to redirect both naked http://domain.com and https://domain.com to https://www.domain.com.

我的.htaccess文件中有此规则,但是当您使用 https://domain.com 明确访问网站时,它涵盖了重定向. a>,但是在访问 http://domain.com 时,它会重定向到 https://www.domain.com .

I have this rules in my .htaccess, but it covers redirect when you visit site explicitly with https://domain.com, but, when visiting http://domain.com, it redirects to http://www.domain.com instead https://www.domain.com.

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

推荐答案

我个人将这样布置,这将强制对所有内容使用WWW和HTTP.

I personally would lay it out like this, this will force WWW and HTTPs for everything.

RewriteEngine On

#Force WWW on everything
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#Force HTTPS on everything
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

这篇关于.htaccess将裸http和https都重定向到www https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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