Force SSL/https using .htaccess and mod_rewrite

后端 未结 9 1664
一向
一向 2020-11-22 05:24

How can I force to SSL/https using .htaccess and mod_rewrite page specific in PHP.

9条回答
  •  故里飘歌
    2020-11-22 05:46

    Mod-rewrite based solution :

    Using the following code in htaccess automatically forwards all http requests to https.

    RewriteEngine on
    
    RewriteCond %{HTTPS}::%{HTTP_HOST} ^off::(?:www\.)?(.+)$
    RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
    

    This will redirect your non-www and www http requests to www version of https.

    Another solution (Apache 2.4*)

    RewriteEngine on
    
    RewriteCond %{REQUEST_SCHEME}::%{HTTP_HOST} ^http::(?:www\.)?(.+)$
    RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
    

    This doesn't work on lower versions of apache as %{REQUEST_SCHEME} variable was added to mod-rewrite since 2.4.

提交回复
热议问题