WordPress causing “This webpage has a redirect loop” error remotely

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.

Read More

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');
?>

Related posts

Leave a Reply

10 comments

  1. My hosted server settings were forcing mysite.com to http://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!

  2. 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 !

  3. 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');
    
  4. 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!

  5. This error can happen in one of two places, on the user side, or on the admin side.

    When it happens on the admin side (wp-admin), you will find that you are unable to login.

    Step 1: Connect to your blog using ftp and navigate to your wp-content/plugins/ directory and download all of your plugins to a folder on your desktop.
    Step 3: Still in your FTP program, delete all plugin folders and php files in the plugins directory. You do not want anything at all left in this directory.
    Step 4: Try logging into your blog. It should let you log in with no problems. Visit the plugins section of your blog. This should load with a lot of errors telling you that each plugin has been deactivated due to an error. This is ok.
    Step 5: Using your ftp program you should upload all your plugins. Once uploaded you should activate each plugin one by one within wordpress and then visit the wp-admin/ login address to make sure that it will pass you through to your blog once logged in.
    Step 6: When you activate a plugin and start getting this error again, you will know which one is to blame. Simply delete that plugin via ftp and start looking for an alternative plugin to use.

    If these steps do not fix your problem, you should backup all your wordpress files and then delete them. You should them upload clean wordpress files for the latest version and try logging in with no themes or plugins uploaded. You can upload these later once you can login.

    If you continue getting error, clear your browser cache, test logging in with other browsers.

    If all else fails, contact your webhost or open a thread on the wordpress support forum

  6. First of all I’m just a beginner so…
    Meanwhile I think that I may have an alternative/shortcut to this issue.

    Instead of deleting every plugin and then reinstalling all of them one by one I suggest to start by deleting the lasted plugins because “resent problem = recent modification/plugins”…

    That is just my suggestion.

    Marco Davide

  7. Though I found this post and solution really helpful because this problem was driving me crazy and took me so much time to realize. In my case it happened because I moved the files inside the same server but using another domain and after finishing all configurations and replacements I was not able to view pages and got this error.
    In my hosting settings there was a Preferred domain filed which was set to domain.ltd (I tried to change it but finally left it so) and than in Settings->General wordpress dashboard I had WordPress Address (URL) “mysite.com” and Site Address (URL) set to “www.mysite.com” after setting last value without http://www..bum everything is okay now. Hope this helps anyone else

  8. in my situation, locally – Openserver.

    The problem appeared in capital letters in the address. For example, in the site settings it was http://local-Host, it was the capital letters that caused the problem, because the entire address is reduced to lowercase letters – http://local-host, and as result $redirect_url was different from $requested_url in canonical.php

  9. 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.