Wordpress causing “This webpage has a redirect loo

2019-04-08 17:14发布

I have a Wordpress site that works perfectly well in development (at mysite.dev), however when I deploy it to my remote server (mysite.com) it throws the 'This webpage has a redirect loop' error.

I can see in the loading bar that the browser is trying www.mysite.com then mysite.com then www.mysite.com again and again, however I'm not sure if this is relavant or not.

If my Wordpress database configuration is incorrect, I get the Error establishing a database connection message, however when everything is set correctly it breaks in this re-direct loop thing.

I have changed the field in the database (siteurl) to reflect the remote settings (http://mysite.com/wordpress).

Note: My wordpress files are stored in a folder called wordpress in my root directory except for wp-config.php, index.php and .htaccess.

Any ideas?

.htaccess contents:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

index.php:

<?php
/**
 * @package WordPress
 */
define('WP_USE_THEMES', true);

require('./wordpress/wp-blog-header.php');
?>

9条回答
不美不萌又怎样
2楼-- · 2019-04-08 18:11

I had the same problem, the answer was the .htaccess file and the redirects there. It seems that the theme/plugins I was using wanted its own code which was similar to that on the wordpress site for htaccess. I looked through the plugin and found the code it referenced and changed that in the htaccess file. Now it works.

查看更多
看我几分像从前
3楼-- · 2019-04-08 18:12

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 !

查看更多
叼着烟拽天下
4楼-- · 2019-04-08 18:13

Are you using a plugin like Redirection which tracks URL changes? This might have created a loop where it redirects mysite.dev/page/ to mysite.com/page/ and mysite.com to mysite.dev. Happened to me before!

查看更多
登录 后发表回答