Thursday, July 3, 2008

Ways to Force HTTPS on HTTP

  1. Add this to your httpd.conf:
    SSLRequire %{HTTP_HOST} eq "www.mydomain.com"
    
  2. Or try this:
    # Turn on Rewriting
    RewriteEngine on 
    
    # Apply this rule If request does not arrive on port 443
    RewriteCond %{SERVER_PORT} !443 
    
    # RegEx to capture request, URL to send it to (tacking on the captured text, stored in $1), Redirect it, and this is last rule.
    RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R,L]
    
  3. Append the following line to httpd.conf:
    Redirect permanent / https://www.mydomain.com/
    
  4. Or try this:
    <NameVirtualHost 122.123.124.1:80>
        ServerName mywebsite.com:80
        ServerAlias http://www.mywebsite.com:80
        ServerAlias 122.123.124.1:80
        Redirect permanent / https://www.mywebsite.com/
    </VirtualHost>
    
  5. Add the following lines to .htaccess:
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
    Make sure you have this line in your httpd.conf file:
    LoadModule rewrite_module modules/mod_rewrite.so
    
  6. Or try this:
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R,L]
    
Note: Restart Apache for new configuration to take effect.
$ /etc/init.d/httpd restart