使用https

时间:2018-02-04 16:44:43

标签: .htaccess ssl redirect https

如何使用https将所有www请求重定向到非www?

例如以下所有3个URL:
http://www.example.com/about-us
http://example.com/about-us
https://www.example.com/about-us

应重定向到https://example.com/about-us

我已经尝试过发布的答案 redirection issue in www to non www with ssl/https

.htaccess redirect www to non-www with SSL/HTTPS但他们没有工作。我目前的规则是

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

请指导。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码“

RewriteEngine On
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} ^www\. 
RewriteRule ^(.*)$ https://example.com/$1 [L,NE,R=302]

因此,上面的方案是在此条件http的情况下捕获所有www是否包含RewriteCond %{HTTPS} off,然后使用此条件捕获所有www RewriteCond %{HTTP_HOST} ^www\.然后强制所有进入https://

注意:清除浏览器缓存并对其进行测试,如果确定,请将302更改为301以获得永久重定向。

更新:

转到.htaccess

中的这些行
# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

在没有#的情况下制作它们:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

然后直接转到页面末尾的代码:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]

将其更改为:

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]

测试它并确保清除浏览器缓存。

如果没问题,请按我上面的说法将302更改为301