HTTPS
provides encrypted traffic with the use of SSL
or TLS
. As websites are moving away from HTTP
to HTTPS
, it's generally a good idea to automatically redirect visitors of the HTTP
version of your website to the HTTPS
version.
This can be done using Apache
whether using the Redirect
directive in your Apache
's VirtualHost
configuration or by using htaccess
file.
Methods to redirect HTTP to HTTPS in Apache:
rewrite
module for Apache
. $ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
a2enmod
support can simply run the command above without having to manually enable the required modules.CentOS
and Red Hat
enables the module by default so requires no manual action to enable the modules.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 |
.htaccess
file on the web folder where you want to set the redirection from using your preferred text editor. $ sudo vi /var/www/html/.htaccess
.htaccess
file. RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://www.simplified.guide/$1 [R,L]
rewrite
module for Apache
. $ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
a2enmod
support can simply run the command above without having to manually enable the required modules.CentOS
and Red Hat
enables the module by default so requires no manual action to enable the modules.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 |
VirtualHost
config that you want to set up the redirection from using your favorite text editor. $ sudo vi /etc/apache2/sites-enabled/000-default.conf
RewriteRule
and related directive in the VirtualHost
configuration just as the htaccess
method.<VirtualHost *:80> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://www.simplified.guide/$1 [R,L] </VirtualHost>
Apache
for the changes to take effect.
VirtualHost
config for HTTP
that you want to set up the redirection from using your favorite text editor. $ sudo vi /etc/apache2/sites-enabled/000-default.conf
VirtualHost
configuration to redirect to the HTTPS
URL
. <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.
Apache
for the changes to take effect. Guide compatibility:
Operating System |
---|
Ubuntu 16.04 LTS (Xenial Xerus) |
Ubuntu 16.10 (Yakkety Yak) |
Ubuntu 17.04 (Zesty Zapus) |
Ubuntu 17.10 (Artful Aardvark) |
Ubuntu 18.04 LTS (Bionic Beaver) |
Ubuntu 18.10 (Cosmic Cuttlefish) |
Ubuntu 19.04 (Disco Dingo) |
Comment anonymously. Login not required.