Redirect http to https

From vpsget wiki
Jump to: navigation, search

In order to redirect http to https in Apache it's enough to add these lines to .htaccess file in the web root:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

To redirect www http to non-www https, add these lines to .htaccess:

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

Please note that for these directives to work Apache conf file should contain allowing rules for the web directory:

<Directory />
   Options FollowSymLinks
   AllowOverride All            

If you have nginx, then you can edit http section of main.conf file:

server {
    listen 80;
    rewrite (.+)$1 permanent;