How to redirect to https in WordPress?

I’ve just purchased an SSL certificate for a WordPress site and want people to go to https://www… rather than http://www..

I’ve read that you need to change the Change the WordPress Address (URL) and Site Address (URL) within the dashboard in Settings – General.

Read More

I also read that you need to add in a redirect within an .htaccess file with:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.net/$1 [L,R=301,NC]

Do I need to do both?

If yes, does the above code get placed within the default WordPress .htaccess code or would it appear above it?

Thanks

Related posts

4 comments

  1. try adding this on to your functions.php instead of changing something in to your .htaccess

    add_action('template_redirect', 'redirect_core', 50);
    add_action('init', 'redirect_core', 50);
    add_action('wp_loaded', 'redirect_core', 50);
    function redirect_core(){
      if (!is_ssl()) {
        wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 301);
        exit();
      }
    }
    

    or refer to this plugin: HTTP Redirect

  2. I am not much use at .htaccess rules, but I know that’s not the rule I use. In conjunction with changing the WordPress Address and Site Address (as you point out), I change the default WordPress .htaccess to

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    

    However if it’s an existing site you are going to run into the issue that your database is riddled with content full of image links using the http protocol which will break your padlock in the browser. Rather than editing all the posts, I use this tool;

    https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

    (Most folk use it when transferring a WP site from one domain to another) and just rename all the images in the database by replacing all instances of http://www.example.com with https://www.example.com

  3. you can run code inside your functions.php

    function redirectToHTTPS(){
     if($_SERVER['HTTPS']!="on"){
       $redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        header("Location:$redirect");   
    }}
    add_action('init','redirectToHTTPS');
    

    You can also use code inside .htaccess

    RewriteEngine On RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

    Both works, but use ony one, not both.

  4. Put this code in .htaccess file 
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteBase /blog/
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /blog/index.php [L]
    </IfModule>
    

Comments are closed.