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

时间:2020-01-30 13:02:23

标签: wordpress .htaccess cpanel

我已经尝试在.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>

4 个答案:

答案 0 :(得分:0)

尝试一下:

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]

来自here

答案 1 :(得分:0)

您可以使用此:

RewriteEngine on

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

在进行测试之前,请确保清除浏览器缓存。

答案 2 :(得分:0)

我在一个Safari拒绝使用http从https重定向到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]

答案 3 :(得分:0)

这对我有用。试试这个

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]