首页 文章

HTACCESS使用HTTPS重定向添加WWW

提问于
浏览
3

目前我的htaccess代码是

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


#send all traffic to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

这在输入以下网址时有效

1. https://example.com -> https://www.example.com
2. http://example.com -> https://www.example.com
3. http://www.example.com -> https://www.example.com
4. https://example.com -> https://www.example.com
5. https://example.com/somepage -> https://www.example.com/somepage
6. http://www.example.com/somepage -> https://www.example.com/somepage

但是当尝试访问某个页面时,https和www都不存在时,它不起作用,而是重定向到奇怪的URL

7. http://example.com/somepage -> https://www.example.com/https://example.com/somepage

Any suggestions?

1 回答

  • 6

    用这个替换你当前的代码

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

    注意:也许你'll have to clear your browser'的缓存看到它适用于 http://example.com/somepage

相关问题