Force Apache to Always Use HTTPS/SSL CentOS 6
In this blog we have already setup apache to run as SSL, now we want to force ALL traffic to use SSL, rather than port 80.
We are assuming you completed the steps from this page
That is our Configure Apache for SSL/HTTPS on CentOS 6 guide: mod_ssl is installed, a certificate is configured and the site already answers on https://. Check that before you add a redirect, otherwise every visitor is sent to a page that does not load.
The simplest and fastest way to force HTTPS is mod_alias’s Redirect permanent: Apache answers every plain HTTP request with a 301 status pointing at the same path on https://, and browsers and search engines remember the new address.
Prerequisites
- Apache (httpd) with a working HTTPS site on port 443.
- Root access to edit
/etc/httpd/conf/httpd.conf. - The exact host name the certificate was issued for, used as the redirect target.
Main Website only
Let’s get Started, edit the primary Apache configuration file
vi /etc/httpd/conf/httpd.conf

Main Website only:

This is to set the global name, and redirect all
ServerName www.example.com:80
Redirect permanent / https://www.example.com/
Redirect permanent / URL matches every path that starts with / (so all of them) and appends the rest of the path to the target, so http://www.example.com/blog/page goes to https://www.example.com/blog/page. Ending the target with a slash keeps paths joined correctly.
One caution: virtual hosts can inherit directives from the main server context. If your HTTPS site is the default _default_:443 virtual host from ssl.conf and it starts redirecting to itself (the browser reports too many redirects), use the virtual host method below instead.
restart Apache, and Confirm Changes
service httpd restart

Virtual Hosts
Virtual Hosts:
Edit the Apache Config

find the virtualhost for example you want to force HTTPS
<VirtualHost *:80>
ServerName virtualhost.zwiegnet.com
Redirect permanent / https://virtualhost.zwiegnet.com/
</VirtualHost>

Note the space between / and https://; the screenshot shows them run together, and Apache needs both arguments. Put the redirect only in the port 80 virtual host. The matching <VirtualHost *:443> block keeps your DocumentRoot and SSL settings.
and restart Apache, verify all works
service httpd restart

Verify the redirect
Before restarting, apachectl configtest (or httpd -t) catches typos and should print Syntax OK. After the restart, check the response headers from any machine:
curl -I http://www.example.com/test
You should see HTTP/1.1 301 Moved Permanently and Location: https://www.example.com/test. Browsers cache 301s aggressively, so test in a private window while you experiment.
Alternative with mod_rewrite
If one virtual host serves several names, or you only want to redirect when the request is not already HTTPS, mod_rewrite keeps the requested host name:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Troubleshooting
- Too many redirects: the HTTPS virtual host is also redirecting. Make sure the Redirect lives only in the
*:80block, or that a proxy or load balancer in front is not sending traffic to Apache over plain HTTP. - Certificate warning after the redirect: the redirect target does not match the certificate name (for example example.com instead of www.example.com).
- Apache will not start: run
apachectl configtest; a Redirect with a missing argument is a common cause.
On RHEL, AlmaLinux and Rocky Linux 8, 9 and 10
The same directives work on Apache 2.4. Put the port 80 virtual host in its own file such as /etc/httpd/conf.d/example-redirect.conf instead of editing httpd.conf, open both services with firewall-cmd --permanent --add-service=http --add-service=https and firewall-cmd --reload, and restart with systemctl restart httpd. If you use Let’s Encrypt, certbot’s Apache plugin can add the redirect for you. Once HTTPS works for every page, consider adding an HSTS header in the 443 virtual host.
That’s it!
Hosted Linux Servers at www.zwiegnet.com/go
Related guides: Install Apache on CentOS 6.4 | Disable Apache Version
