Proper Apache redirection from http to https

22,308

To redirect to HTTPS:

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

But you need to have a virtual host for ssl:

NameVirtualHost *:443
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    SSLEngine on
    SSLCertificateFile    /etc/apache2/ssl/apache.crt
    SSLCertificateKeyFile /etc/apache2/ssl/apache.key
</VirtualHost>

NOTE: you have to create a SSL certificate... but there is great tutorial for this!

Share:
22,308

Related videos on Youtube

vaindil
Author by

vaindil

Updated on September 18, 2022

Comments

  • vaindil
    vaindil almost 2 years

    I set up a new virtual host for a new site on my server, but it's acting strangely despite having the exact same settings as my original site.

    I want all requests to be redirected to HTTPS, simply put. On my main site (the working one) I have this specified in the VirtualHost config, and in .htaccess I have an additional rule specified to allow short URLs. Specifically, the problem on the non-working site is that if I try to go to example.com/url, the redirect goes to https://example.comurl and removes the necessary / from the URL.

    I copied the exact config over to the new VirtualHost and .htaccess file from the working site so I'm not sure why it doesn't work on the new one. My DNS records for both sites all use A records to point to it, no redirects or anything happen at the DNS level. I have tried putting a / at the end of the Redirect lines in the code below, but the problem was not resolved. The server is Ubuntu 14.04, and Apache is version 2.4.7. Both sites are separate domain names with separate .conf files used, but they are hosted on the same server with the same IP address. How can I fix this problem?

    The relevant code in the VirtualHost:

    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        Redirect permanent / https://example.com
    </VirtualHost>
    

    And in .htaccess:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
    

    In case it helps/matters, this is the SSL config in my VirtualHost file, though it is placed at the very beginning of the file outside of all other directives:

    SSLCipherSuite AES128+EECDH:AES128+EDH
    SSLProtocol All -SSLv2 -SSLv3
    SSLHonorCipherOrder On
    Header always set Strict-Transport-Security "max-age=63072000; includeSubdo$
    Header always set X-Frame-Options DENY
    Header always set X-Content-Type-Options nosniff
    
    SSLCompression off
    SSLUseStapling on
    SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
    

    And this is the config specifically for the VirtualHost for the site:

    <VirtualHost *:443>
        ServerName example.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/example
        ErrorLog ${APACHE_LOG_DIR}/errorexample.log
        Options -Indexes
    
        SSLEngine on
        SSLCertificateFile /etc/ssl/example/examplecert.crt
        SSLCertificateKeyFile /etc/ssl/example/examplekey.key
        SSLCertificateChainFile /etc/ssl/chain/class1.pem
    </VirtualHost>
    
    • td512
      td512 about 9 years
      have you got SSL defined?
    • Marki555
      Marki555 about 9 years
      Maybe different Apache versions? And have you tried putting the slash at the end of the Redirect line? Like this? Redirect permanent / https://example.com/
    • vaindil
      vaindil about 9 years
      I do have SSL defined, I added the relevant code blocks to the OP. I also did try putting a / at the end of the Redirect command, but that did not fix the problem.
  • vaindil
    vaindil about 9 years
    Huh, this appears to have resolved the problem. I already had the SSL virtual host specified (I suspect you were writing this answer while I was editing mine to add that in), but I changed my Redirect lines to your first code block instead and everything works properly now. Thank you so much!
  • Ricain
    Ricain about 9 years
    happy to help :)
  • Nullpointer
    Nullpointer over 7 years
    Can you check this link :askubuntu.com/questions/876767/…
  • Andrew
    Andrew almost 7 years
    where does the rewrite engine stuff go, in the vhosts file?