0

Recently this solution was given (Thanks Mr.D), for this Wordpress redirect to landing page if not logged in thread

<?php
if($_SERVER['REQUEST_URI'] != '.../' || $_SERVER['REQUEST_URI'] != '.../'){

if(!is_user_logged_in()) {
wp_redirect( 'http://www.mysite.com/landingpage', 301 ); exit;
}
}

I recently tried this piece of code and it works if i substitute say "http://www.google.com"

However, if I substitute it with mydomain.com/landingpage i get a redirect loop error.

Any ideas?

Thanks!

| improve this question | | | | |
1

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'], '/' ) != 'landingpage' && ! is_user_logged_in() ) {
    wp_redirect( home_url( 'landingpage' ), 301 );
    exit;
}
| improve this answer | | | | |
  • Thanks so much! This was a huge help, also thanks for explaining the problem and not just providing the solution. – icemanhx Dec 9 '12 at 3:20

Your Answer

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

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