Howto force SSL for all requests?

Is there a way to force SSL for all requests? Much like the option to use admin ssl, but for all requests, including the ones who are not logged in.

Related posts

Leave a Reply

4 comments

  1. A simple check for is_ssl() should do it:

    add_action( 'plugins_loaded', 'wpse_2718_force_ssl' );
    
    function wpse_2718_force_ssl()
    {
        if ( is_ssl() )
            return;
    
        wp_redirect(
            'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] 
        );
        exit;
    }
    

    But I would do this in .htaccess to catch images too:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    For ISS see this answer on Stack Overflow.

  2. Add this rule to the top of .htaccess:

    # BEGIN Force SSL
    # This should be the first rule before other rules
    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    </IfModule>
    # END Force SSL
    

    This should be before WordPress’ rules.