apachemod-rewritehttpsvirtualhost

Apache shows default page instead of https when using http


I'm trying to redirect all my foo.mydomain.com requests to https://foo.mydomain.com.

My VirtualHost contains this:

<VirtualHost *:443>
    ServerName foo.mydomain.com

    # Indexes + root dir
    DirectoryIndex index.html
    DocumentRoot /home/web/foo.mydomain.com/htdocs

    SSLEngine on

    SSLCertificateFile /etc/apache2/ssl/server.crt
    SSLCertificateKeyFile /etc/apache2/ssl/server.key

    SSLVerifyClient none

    <IfModule mime.c>
            AddType application/x-x509-ca-cert      .crt
            AddType application/x-pkcs7-crl         .crl
    </IfModule>

    # Logfiles
    ErrorLog /home/web/foo.mydomain.com/logs/error.log
    CustomLog /home/web/foo.mydomain.com/logs/access.log combined
</VirtualHost>

So now I'm trying to redirect all the http-request to https with mod_rewrite in this .htaccess file:

Options FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://foo.mydomain.com

Does anyone knows what I'm doing wrong? Thank you in advance!


Solution

  • Try the following in the VirtualHost config for the non HTTPS site.

    I.e. my settings to do this are:

    <VirtualHost *:80>
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    .....
    </VirtualHost>
    

    This should redirect all incoming HTTP requests to HTTPS. Then in the config for port 443 you can do whatever you need.

    Note: should work in htaccess as well.

    Hope that helps. Not sure what you meant by 'default page' but in title and too low rep to leave comments...

    Let me know...

    Cheers

    Robin