Use .htaccess to redirect HTTP to HTTPs

后端 未结 17 1558
刺人心
刺人心 2020-12-02 05:51

Please, don\'t recommend me the long and very detailed thread with more than 173 upvotes. It didn\'t work for me. I have also tried many others (1, 2, 3, 4). They all give m

相关标签:
17条回答
  • 2020-12-02 06:32

    It works for me:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !on           
    RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    
    0 讨论(0)
  • 2020-12-02 06:33

    Here is an alternative solution you can use if you don't want to edit .htaccess:

    add_action( 'template_redirect', 'nonhttps_template_redirect', 1 );
    
    function nonhttps_template_redirect() {
    
        if ( is_ssl() ) {
    
            if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'https' ) ) {
    
                wp_redirect( preg_replace( '|^http://|', 'https://', $_SERVER['REQUEST_URI'] ), 301 );
    
                exit();
    
            } else {
    
                wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301 );
    
                exit();
    
            }
    
        }
    
    }
    

    You can place this at the bottom of your theme functions.php

    0 讨论(0)
  • 2020-12-02 06:36

    In my case, the htaccess file contained lots of rules installed by plugins like Far Future Expiration and WPSuperCache and also the lines from wordpress itself.

    In order to not mess things up, I had to put the solution at the top of htaccess (this is important, if you put it at the end it causes some wrong redirects due to conflicts with the cache plugin)

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    

    This way, your lines don't get messed up by wordpress in case some settings change. Also, the <IfModule> section can be repeated without any problems.

    I have to thank Jason Shah for the neat htaccess rule.

    0 讨论(0)
  • 2020-12-02 06:36

    Nothing of the above worked for me. But those lines solved the same problem on my WordPress site:

    RewriteEngine On
    
    RewriteCond %{HTTP:HTTPS} !on
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    0 讨论(0)
  • 2020-12-02 06:39

    I found all solutions listed on this Q&A did not work for me, unfortunately. What did work was:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
    RewriteRule ^(.*\.php)$ $1 [L]
    RewriteRule . index.php [L]
    
    RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com$
    RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
    </IfModule>
    # End Wordpress
    

    Note, the above Wordpress rules are for Wordpress in multi user network mode. If your Wordpress is in single site mode, you would use:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com$
    RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
    </IfModule>
    # End Wordpress
    
    0 讨论(0)
提交回复
热议问题