首页 文章

使用https将www URL重定向到非www

提问于
浏览
0

如何通过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

    请尝试以下代码“

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

    所以,上面的场景是通过这个条件 RewriteCond %{HTTPS} off 捕获所有 http 有或没有 www 然后用这个条件 RewriteCond %{HTTP_HOST} ^www\. 捕获所有 www 然后强制全部进入 https:// .

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

    Update :

    转到 .htaccess 中的这些行:

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

    像这样在没有_1070266的情况下制作它们:

    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 ,如上所述 .

相关问题