• Hi Folks,
    I just installed an SSL certificate on my website and I want to redirect all http traffic to https.

    My question is: What is the best way to do this? and considering that for best SEO the redirects should be 301.

    What I’ve tried is:
    1. Edit the .htaccess with the following code:

    # 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]
    </IfModule>
    # END WordPress
    
    # BEGIN Force http to https
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    # END Force http to https

    I didn’t have much luck, it seemed to work with the homepage, but if somebody typed an url with http it did not redirect to its https counterpart.

    2. Edit the .htaccess with the following code:

    # 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]
    </IfModule>
    # END WordPress
    
    # BEGIN Force http to https
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
    # END Force http to https

    Again, it seemed to work with the homepage, but if somebody typed an url with http it did not redirect to its https counterpart.

    3. Finally I installed the plugin WP Force ssl, but Im not sure if the redirects are done in the right way(301 redirects) for better SEO.

    Any thoughts on this? Why does the code in my .htaccess is not working?. How have you managed this redirection for your sites?

    any ideas would be appreciated. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • This goes above your wordpress rewrite rules in .htaccess in site root folder:

    
    # BEGIN SSL
    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_USER_AGENT} ^(.+)$
    RewriteCond %{SERVER_NAME} ^yourdomian\.com$
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
    Header add Strict-Transport-Security "max-age=300"
    </IfModule>
    # END SSL
    
    Thread Starter Edwin Xico (XicoOfficial)

    (@xicoofficial)

    Thank you so much @swansonphotos. It worked.

    I do not like relying on many plugins for this type of functionality, and your answer worked perfectly.

    If you have a link to any online resource where I can learn about the .htaccess file I would appreciate it. I would like to understand what each line of code does and why the code I previously used didn’t work..

    Anyhow, thanks again for your support.

    I had the same issue some time back when switching a client to HTTPS. So I had it in a .htaccess file there.

    I don’t recall where I gathered it from. Refer back to here I guess :-).

    It’s important to understand that you want the site URL to be set to HTTPS and then add these rules above the WordPress rewrite rules. The first set of rules deal with folks who come to the site with an old URL and are not navigating on the site. When they are at the site, all the URL’s should now use HTTPS links (check that).

    So in order:

    1. If not HTTPS, make HTTPS. Server, not application specific.
    2. Rewrite rules for my setting in WordPress. Application specific.

    Glad to help. Please make sure to mark this as resolved so it can be found more by other folks as a resolved topic.

    It also worked for me @swansonphotos

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘What is the best way to implement 301 redirect from http to https?’ is closed to new replies.