Landing Page – Redirect Loop?

What’s happening is you’re sending an unauthenticated visitor to a certain page. The code in that page also picks up that the user is unauthenticated, and sends them to the page that they’re already on. Because the page keeps on sending the visitor to the same page, the visitor can’t go anywhere. This is called a redirect loop.

You’re on the right track with the $_SERVER['REQUEST_URI'] check. However, paths like ../ and .../ won’t work. You need to use the actual URL of the page that you’re redirecting to (sans the domain and protocol). Essentially, you need to use this code:

if ( trim( $_SERVER['REQUEST_URI'], "https://wordpress.stackexchange.com/" ) != 'landingpage' && ! is_user_logged_in() ) {
    wp_redirect( home_url( 'landingpage' ), 301 );
    exit;
}

Leave a Comment