Apache redirect from http to https not working

I have the following conf files:

file1:

NameVirtualHost  123.45.67.890:80

<VirtualHost 123.45.67.890:80>
    ServerName example.com

    RedirectPermanent / https://example.com/

#   RewriteEngine On
#   RewriteCond %{SERVER_PORT} !^443$
#   RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

#   SSLRequireSSL
#   Redirect permanent /secure https://example.com/

#   Redirect / https://example.com/
</VirtualHost>

As you can see from the commented out lines, I have tried several approaches.

file2:

NameVirtualHost 123.45.67.890:443

<VirtualHost 123.45.67.890:443>
    DocumentRoot "/opt/www/example-docroot"
    ServerName example.com
    DirectoryIndex index.html
    SSLEngine on

    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    SSLCertificateFile /etc/httpd/conf.d/ssl/example.com/csr.example.2011.pem.blade
    SSLCertificateKeyFile /etc/httpd/conf.d/ssl/example.com/nokey.example.2011.pem
    SSLCACertificateFile /etc/httpd/conf.d/ssl/example.com/CA.blade.2011.csr

    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
        SSLOptions +StdEnvVars
    </Files>
    <Directory "/etc/httpd/cgi-bin">
        SSLOptions +StdEnvVars
    </Directory>
</VirtualHost>

Some thing removed for simplicity, names changed to protect the innocent.

I update these files on the server /etc/httpd/vhosts

then run

apachectl restart

Which does give me these warnings:

[warn] NameVirtualHost 123.45.67.890:80 has no VirtualHosts
[warn] NameVirtualHost 123.45.67.890:80 has no VirtualHosts

We have numerous vhosts running from this server. The above configs seem to be aligned, so I don't think these warnings are applying here. Maybe wrong.

Updating the default 80 (http) page in my browser, always shows the default http page.

Any suggestions, on how to get the redirect to work?


Solution 1:

Many examples work on specific configurations. This one always works, no matter which configuration your Apache server uses:

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]

Solution 2:

I had faced exactly the same issue few days back. I tried the following in my VirtualHost config (applicable for http port 80) in apache httpd.conf file that worked.

<Virtualhost *:80>
ServerAdmin [email protected]
ServerName site.com
ServerAlias site.com www.site.com

RedirectMatch permanent ^(.*)$ https://www.site.com$1
</Virtualhost>

This works like charm and you don't need any config anywhere else or any extra module.

Solution 3:

Better use .htaccess for this (if possible), no need to mess with the apache config files. Add these lines to the beginning of your .htaccess.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Solution 4:

I had the same issue on apache 2.4 on centos 7 and the fix that worked for me without using the RewriteEngine was to add :443 to the ServerName directive under the VirtualHost for https.

<VirtualHost 123.45.67.890:443>
DocumentRoot "/opt/www/example-docroot"
ServerName example.com:443