use htaccess to add www with https support

2019-01-18 15:55发布

There are a lot of code examples for using .htaccess to add www to a url but they are usually confined to using http. does anybody have an example of how to get it to work in both cases?

2条回答
对你真心纯属浪费
2楼-- · 2019-01-18 16:10

use this code in .htaccess and it will add both https and www

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
查看更多
Lonely孤独者°
3楼-- · 2019-01-18 16:14

Sure:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The second condition checks if the HTTPS environment variable (either on or off) is set to on and captures the appended s that is then available with %1. If it doesn’t match, %1 is just an empty string.

查看更多
登录 后发表回答