Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What is the best way to always reroute all traffic from http to https on Apache 2?

All requests to 80 should go to 443.

(I am using Ubuntu + Apache 2)

share|improve this question
    
That's going to depend on whether you want to do it in the Apache conf or with htaccess. Oscar's reply partially addresses the htaccess method. You'll need to have the mod rewrite enabled. BMdan and freiheit addressed the apache conf (trying to be clear for the newbs) –  Patrick R Jul 29 '10 at 19:28

3 Answers 3

up vote 3 down vote accepted

Expanding on freiheit's answer, removing some redundant parts, eliminating the dependence on mod_rewrite (and associated speed penalty), and adding in some security:

<VirtualHost 1.2.3.4:80>
        ServerName SSL.EXAMPLE.COM
        CustomLog /var/log/httpd/EXAMPLE.access_log combined
        ErrorLog /var/log/httpd/EXAMPLE.error_log

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

<VirtualHost 1.2.3.4:443>
    ServerName ssl.example.com
    DocumentRoot /var/www/html
    CustomLog /var/log/httpd/EXAMPLE.ssl_access_log combined
    ErrorLog /var/log/httpd/EXAMPLE.ssl_error_log
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite HIGH:MEDIUM
    .
    .
    .
</VirtualHost>

<Directory /var/www/html>
    #If all else fails, this will ensure nothing can get in without being encrypted.
    SSLRequireSSL
</Directory>
share|improve this answer
    
This worked for me, thank you. –  Daniel Jul 31 '10 at 18:47

Personally, my favorite is to simply redirect all port 80 traffic to port 443:

<VirtualHost 1.2.3.4:80>
        ServerName SSL.EXAMPLE.COM
        ServerAlias SSL
        DocumentRoot /var/www/html
        CustomLog /var/log/httpd/EXAMPLE.access_log combined
        ErrorLog /var/log/httpd/EXAMPLE.error_log

        RewriteEngine on
        RewriteRule .* https://SSL.EXAMPLE.COM/ [NC,R,L]
</VirtualHost>

<VirtualHost 1.2.3.4:443>
    ServerName ssl.example.com
    DocumentRoot /var/www/html
    CustomLog /var/log/httpd/EXAMPLE.ssl_access_log combined
    ErrorLog /var/log/httpd/EXAMPLE.ssl_error_log
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite HIGH:MEDIUM
    .
    .
    .
</VirtualHost>
share|improve this answer
1  
If you want to use mod_rewrite here, you should add the "QSA" flag. It'd be simpler to use mod_alias, however. I've posted an answer modified as such. It's also not a good idea to specify the non-SSL host's DocRoot as the SSL host's DocRoot; if someone gets past your Redirect, they're now browsing the SSL site without SSL. Finding an appropriate empty directory is left as an exercise to the reader, or you can add a <Directory /> stanza inside the non-SSL VHost with "deny from all" or similar if you'd prefer. –  BMDan Jul 29 '10 at 17:35

RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/(.*) https://your.server.name/$1 [R]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.