Automatically Redirect HTTP to HTTPS in WordPress

How to redirect wordpress from http to https; Automatic http to https redirect wordpress; Wordpress force http to https; Redirect wordpress site to https; Wordpress https redirect plugin; Http to https wordpress htaccess; Wordpress ssl redirect setup; Easy https redirection wordpress; Wordpress http to https migration; Convert wordpress http to https;

Explanation

To make sure your WordPress site always uses a secure connection (HTTPS), you can use this handy piece of code. It automatically redirects any HTTP requests to their HTTPS counterparts. Here's how it works:

  • Check for Security: The code first checks if the current request is not secure using is_ssl(). If it's not secure, it means the request is using HTTP instead of HTTPS.
  • Build the Secure URL: It then constructs the secure version of the current URL by adding 'https://' in front of the current host and request URI. This ensures that the URL is correctly formatted for HTTPS.
  • Redirect to HTTPS: Finally, it uses wp_redirect() to send the user to the HTTPS version of the page with a 301 status code, which tells browsers and search engines that this is a permanent redirect.

By adding this code to your theme's functions.php file, you ensure that visitors are always directed to the secure version of your site, enhancing security and trustworthiness.

Code

add_action('template_redirect', 'wp_dudecom_force_https');

function wp_dudecom_force_https() {
    // Check if the request is not secure
    if (!is_ssl()) {
        // Get the current URL
        $current_url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        
        // Redirect to the HTTPS version of the URL
        wp_redirect($current_url, 301);
        exit;
    }
}

Instructions

To implement automatic redirection from HTTP to HTTPS on your WordPress site, follow these steps:

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure your hosting provider supports SSL and you have an SSL certificate installed.
  • Verify that your WordPress Address (URL) and Site Address (URL) in Settings > General are set to use HTTPS.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are adding the code to functions.php. Alternatively, navigate to Plugins > Add New and create a new plugin if you prefer using a custom plugin.
  3. Locate and open the functions.php file from the right-hand side file list if using the Theme Editor.
  4. Copy and paste the provided code snippet at the end of the functions.php file or your custom plugin file:
  5. 
        add_action('template_redirect', 'wp_dudecom_force_https');
    
        function wp_dudecom_force_https() {
            if (!is_ssl()) {
                $current_url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                wp_redirect($current_url, 301);
                exit;
            }
        }
        
  6. Save the changes to the file.
  7. Test your website by accessing it via HTTP (e.g., http://yourwebsite.com) to ensure it redirects to HTTPS (e.g., https://yourwebsite.com).

By following these steps, you ensure that all visitors are automatically redirected to the secure HTTPS version of your site, enhancing security and user trust.

If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.