1. Introduction
HTTP (Hyper Text Transfer Protocol) is a popular as well as the fundamental protocol for data communication on the World Wide Web (WWW); typically between a web browser and the server which stores web files. Whereas HTTPS is the secure version of HTTP, where the ‘S‘ at the end stands for ‘Secure‘.
2. Redirect HTTP to HTTPS on Apache Using .htaccess File
For this method, make sure mod_rewrite is enabled:
For Ubuntu or Debian :
$ sudo a2enmod rewrite
For Centos or RHEL :
For CentOS/RHEL users, ensure that your have the following line in httpd.conf (mod_rewrite support – enabled by default).
LoadModule rewrite_module modules/mod_rewrite.so
Now you just need to edit or create .htaccess file in your domain root directory and add these lines to redirect http to https.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
3. Redirect HTTP to HTTPS on Apache Virtual Host
Edit website VirtualHost in Apache configuration file and add the following options. Change www.example.com with your actual domain name.
With this second option you can configure your virtual host file. Normally, there are two important sections of a virtual host configurations if an SSL certificate is enabled; the first contains configurations for the non-secure port 80.
The second is for the secure port 443. To redirect HTTP to HTTPS for all the pages of your website, first open the appropriate virtual host file. Then modify it by adding the configuration below.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect / https://www.yourdomain.com
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
Save and close the file, then restart the HTTP sever like this.
for Ubuntu/Debian :
sudo systemctl restart apache2
for RHEL/CentOS :
sudo systemctl restart httpd
Leave a Reply