====== Apache - Redirect HTTP to HTTPS ======
===== Redirect your visitors to the HTTPS version of your website =====
Edit your apache configuration file (**/etc/apache2/sites-enabled/website.conf** and **/etc/apache2/httpd.conf** for example).
[...]
ServerName example.com
Redirect permanent / https://example.com/
If you only redirect, you don't even need a document root.
Now restart Apache.
===== Using modrewrite to redirect visitors to the HTTPS version of your website =====
You can also use **modrewrite**, however the above method is simpler and safer. However, **modrewrite** below redirects the user to the page they were visiting over https, the above config just redirects to /:
[...]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
And don't forget to restart Apache.
===== To set a redirect in your .htaccess =====
## Disable https on public site. Force redirect to http.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^(.*)/administrator/(.*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} .
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
----
===== Redirect HTTP to HTTPS and add www with each URL =====
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
It will first redirect HTTP to HTTPS and then it will redirect to https://www
----