I am trying to force redirecting all HTTP traffic to HTTPS using my httpd.conf file on my Apache server.
Expected behaviour: When users visit http://example.com, they should get redirected to https://example.com/.
Actual behaviour: When users visit http://example.com, they stay at the same link and an error message is shown by the browser (This site can’t be reached, http://example.com/ refused to connect).
This is my httpd.conf code:
Listen 443
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName "example.com"
ServerAlias "www.example.com"
Redirect permanent / "https://example.com/"
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</VirtualHost>
I also have a httpd-le-ssl.conf file which defines configs for my HTTPS port. The HTTPS link works perfectly fine, I had installed the certificate using certbot.
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
ServerName "example.com"
ServerAlias "www.example.com"
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
What I Have Tried:
- Tried removing "Redirect permanent" and keeping only the RewriteEngine rules in httpd.conf
- Tried removing RewriteEngine rules and keeping only "Redirect permanent" rule in httpd.conf
- Tried keeping both in .htaccess file in my document root /var/www/html
- Tried keeping both in .htaccess file in /var/www/
Links:
- Full httpd.conf source code