繁体   English   中英

将所有流量重定向到 https:// 非 www

[英]Redirect all traffic to https:// non-www

我在 .htaccess 中尝试了几种不同的重定向配置,将所有流量重定向到我网站的 https 非 www URL,但无法让https://www.example.com重定向到https://example.com

明确地说,我想要:

    http://example.com
    http://www.example.com
    https://www.example.com

重定向到

    https://example.com

http 到https://example.com工作正常

编辑:

我不记得我使用的各种组合。

这是我目前设定的

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 
</IfModule>

尝试这个:

RewriteEngine On

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]

# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这里开始

你可以使用这个:

RewriteEngine on

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

确保在测试之前清除浏览器缓存。

我在 Safari 拒绝使用 http 重定向到 https 的域上遇到问题

RewriteCond %{HTTPS} off

我现在有这个工作 .htaccess 内容:

#  Force https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

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

这对我有用。 尝试这个

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

暂无
暂无

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

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