Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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;
}
share|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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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