繁体   English   中英

http到https重定向以及www到htaccess中的非www

[英]http to https redirect along with www to non-www in htaccess

我正在尝试将我的网站从http重定向到https,同时从www重定向到非www。 安德龙的代码在这里工作,但有一些故障。 我稍微调整了它,并重定向了从非https到https的所有内容。

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

对于以下情况,重定向可以完美地工作并重定向到non-www https版本:

domain.com
www.domain.com
www.domain.com/page

但是,当我进入domain.com/pagehttp://domain.com/page我只看到non-secure http non-www版本。

如何确保所有URL重定向到网站的secure https non-www版本?

以下是在单个规则中同时执行http->httpswww删除的规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

为了SEO目的,最好避免多次重定向。

您可以使用:

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM