HTTPS provides encrypted traffic with the use of SSL or TLS. As websites are moving away from HTTP to HTTPS, it's good to automatically redirect the HTTP traffic (normally on port 80) to HTTPS (normally on port 443). It's a foolproof way to secure the connection of those who mistakenly access your HTTP website.
You can automatically redirect HTTP requests to HTTPS in Apache using either a .htaccess file, RewriteRule directive, or Redirect directive.
Methods to redirect HTTP to HTTPS in Apache:
$ sudo a2enmod rewrite #Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
Options | Debian, Ubuntu | openSUSE and SLES | Fedora Core, CentOS, RHEL | macOS | homebrew | xampp |
---|---|---|---|---|---|---|
a2enmod support | yes | yes | no | no | no | no |
Modules to install | none | |||||
Module name | n/a | rewrite | ||||
Loadmodule directive | n/a | LoadModule rewrite_module <module_locations>/mod_rewrite.so |
$ sudo vi /var/www/html/.htaccess
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://www.simplified.guide/$1 [R,L]
$ sudo a2enmod rewrite #Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
Options | Debian, Ubuntu | openSUSE and SLES | Fedora Core, CentOS, RHEL | macOS | homebrew | xampp |
---|---|---|---|---|---|---|
a2enmod support | yes | yes | no | no | no | no |
Modules to install | none | |||||
Module name | n/a | rewrite | ||||
Loadmodule directive | n/a | LoadModule rewrite_module <module_locations>/mod_rewrite.so |
$ sudo vi /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://www.simplified.guide/$1 [R,L] </VirtualHost>
Related: How to manage Apache service
$ sudo vi /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80> ServerName simplified.guide Redirect permanent / https://www.simplified.guide/ </VirtualHost>
permanent is equivalent to 301 redirect and you can use temporary instead for 302 redirect.
Related: How to manage Apache service
Comment anonymously. Login not required.