0

I'm pulling my hair out with this one. I've tried 4 or 5 different approaches but none work consistently.

We have a WP site that includes a blog and Woocommerce for wholesale customers. We have a retail site at Shopify. We now need to redirect just the home page to https://shop.example.com if the user IS NOT logged in, and all other pages work as usual (e.g. https://example.com/blog/).

I'm using the Redirection plugin and have tried this regex rule:

^\/(?!wc-ajax)$

The 'wc-ajax' bit is for ajax requests that WC makes to the root url so we need to exclude those.

This rule seemed to work at first but not consistently. It also would redirect for everyone, including wholesale customers, which I don't want.

I also tried this code in a custom plugin:

add_action( 'init', 'my_redirect_home_page_only' );
function my_redirect_home_page_only() {
    if ( !is_user_logged_in() && is_front_page() ) {
        wp_redirect( 'https://shop.example.com' );
        exit;
    }
}

Couldn't get that to work either. I tried 'wp' and 'template_redirect' instead of the init hook as well.

Any ideas?

  • The code you pasted, when you say you couldn't get that to work either, what was the specific problem? – NightHawk Aug 20 at 20:35
  • The specific problem was that it didn't work. I assumed that the init action is too early, so I tried wp and template_redirect actions but those didn't do anything either, it just failed silently AFAIK. – protohominid Aug 22 at 16:54
  • Can't you add your redirect code only with the condition in your header.php? – Pradipta Sarkar Aug 22 at 19:24
  • @PradiptaSarkar - that seems to work! I'm still testing but hopefully that will resolve it. If it does I'll enter the answer soon. Thanks! – protohominid Aug 23 at 20:17
  • Now I'm wondering about SEO implications of this, though... Google doesn't see the redirect as a 301, right? So if Google thinks our home page is example.com and that page is being redirected to shop.example.com, is that an SEO problem? – protohominid Aug 24 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.