apache:redirect_http_to_https
Table of Contents
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).
- /etc/apache2/sites-enabled/website.conf
<VirtualHost *:80> [...] ServerName example.com Redirect permanent / https://example.com/ </VirtualHost>
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 /:
- /etc/apache2/sites-enabled/website.conf
<VirtualHost *:80> [...] <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} </IfModule> </VirtualHost>
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
apache/redirect_http_to_https.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1