Tuesday, January 19, 2021

How to Redirect HTTP Traffic to HTTPS in httpd on CentOS

The easiest way is to do it using the VirtualHost configuration, if you have control over it. 

Edit your virtualhost configuration for that domain, in CentOS it is usually located in /etc/httpd/conf.d/mydomain.conf:

<VirtualHost *:80>
   ServerName www.mydomain.com
   Redirect / https://www.mydomain.com

<VirtualHost _default_:443>
   ServerName www.mydomain.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On

The most important line is the "Redirect" line which will redirect all http traffic to https.

Once done, save the file.

Do not forget to run syntax test of the configuration files.
# httpd -t

And reload the service
# systemctl reload httpd

No comments: