Wordpress causing "This webpage has a redirect loop" error remotely

My hosted server settings were forcing mysite.com to www.mysite.com and this was causing the problem. I turned this setting off and everything works now, i'd still like to know how to make it work with this setting turned on, though!


I had the exact same problem (a redirection loop after migrating to a new server).

But it was not a problem with the configuration values siteurl and home, neither was it an issue with the .htaccess file. I tried all that. And it was also not a problem about the 'www' added to the URL.

After some researchs I found that the loop was caused by infinite calls to the redirect_canonical function (in wp-inclue/canonical.php). But it was not a bug from wordpress. Some Php configs set the $_SERVER['REQUEST_URI'] in a "wrong way" when you acces your root url. Example : On my server, when I go to http://example.com/ the $_SERVER['REQUEST_URI'] is set to '/index.php' instead of just '/' This is confusing for redirect_canonical because this function always try to redirect to the "better" url it knows for a page. And the better url for the root of your site is '/'. On my server, each time redirect_canonical tried to redirect to '/' it failed, and tried again until an infinite redirect loop was found.

To correct this bug, you can either modify your server configuration - I don't personnaly know how to do that, but I know it is possible - or if you can't change it, just add this code in a custom plugin :

/**
 * avoid redirection loop in redirect_canonical if REQUEST_URI 
 * contains '/index.php' 
 **/

/* remove the action set in the hook 'template_redirect' by wordpress */
remove_action('template_redirect', 'redirect_canonical');

/* set a custom action in the hook 'template_redirect' to check REQUEST_URI value */
add_action('template_redirect', 'correct_redirect_canonical');

/* Function to correct the behaviour of redirect_canonical */
function correct_redirect_canonical(){
    if(strstr($_SERVER['REQUEST_URI'],'/index.php')===false){
        redirect_canonical();
    }
}

Hope it helps !


1.- As "My wordpress files are stored in a folder called wordpress" you must edit RewriteBase and RewriteRule like this:

RewriteBase /wordpress/
RewriteRule . /wordpress/index.php [L]

UPDATE

2.- Try cleaning all cookies. This simple action (through Firebug) has solved this problem to me sometimes.

UPDATE 3

3.- Try this /index.php in your root directory:

// index.php file in root directory
chdir('wordpress'); // change dir to WP
include 'index.php'; // execute WP with their normal `index.php`

and leave /wordpress directory as usual (with their normal index.php and .htaccess inside).

I mean don't change any bit of the normal index.php of WP that it is something like this:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');