1

I am trying to force a 301 https redirect with hooking into the 'template redirect' function. I want not doing it a htaccess because when I update my permalinks, htaccess with https redirect disappears.

So with this function I want to do a redirect if the url is loaded with 'http', but nothing happens. Is there anyone who can help me with this?

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

1 Answer 1

0

We can add https in very simple way.... No need to had code all these in any hook....

  1. Log in to wordpress admin
  2. Go to Settings > General
  3. Find WordPress Address (URL) and Replace HTTP with HTTPS
  4. Find Site Address (URL) and Replace HTTP with HTTPS
  5. Click Save Changes

Edit .htaccess file and add bellow code

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^huepixel\.com$ [NC]
RewriteRule ^ https://www.huepixel.com%{REQUEST_URI} [L,R=301]

Install wordpress-https Plugin and configure it....

That's All

5
  • Have done that, but my Wordpress doesn't redirect automatically..
    – Robbert
    Commented Dec 19, 2014 at 13:48
  • Its working fine for me.... Here is my domain huepixel.com Commented Dec 19, 2014 at 13:50
  • You mean it works with the wordpress-https plugin or with no plugin at all?
    – Robbert
    Commented Dec 22, 2014 at 8:22
  • I used wordpress-https plugin Commented Dec 22, 2014 at 14:45
  • I have activated the plugin too and it works! Thanks
    – Robbert
    Commented Dec 22, 2014 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.