WordPress Multisite Network Redirect Loop (302)

Have you been seeing a lot of this lately when trying to access your WordPress Multisite Network Admin pages? TL;DR here is the solution.

Screenshot of chrome's Too Many Redirects error

This page isn’t working
www.yoursite.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

There is a solution! In fact, there are many solutions to this problem available, but as I wasn’t able to find my exact issue, here it goes.

In WordPress Multisite, the Network Admin will be set up under the DOMAIN_CURRENT_SITE variable defined in wp-config.php around the same section as your other multisite installs. In my specific instance, I’m using  www.mysite.com and it looks something like this:

define( 'DOMAIN_CURRENT_SITE', 'www.mysite.com' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

So to parse that a little, www.mysite.com has a SITE_ID of 1, a BLOG_ID of 1, and has a path of /. The path isn’t as important in this case as the IDs, and you’ll see why in a minute.

Another aspect of this issue is that I have my .htaccess file set up to redirect all non-www requests to www. That’s going to look something like this:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Another thing to notice is that at the same time I’m also doing an HTTPS redirect, but I’m not sure this matters much.

The next step is to take a look at your apache access log. In a standard Ubuntu install it should be located somewhere around /var/log/apache2/access.log. Try to tail it, and then try to access your Network Admin page. You should see something that looks like this:

XX.XX.XXX.XXX - - [01/Apr/2018:12:17:50 -0500] "GET /wp-admin/network/ HTTP/1.1" 302 3779 "-" "Your User Agent"
XX.XX.XXX.XXX - - [01/Apr/2018:12:17:50 -0500] "GET /wp-admin/network/ HTTP/1.1" 302 453 "-" "Your User Agent"
XX.XX.XXX.XXX - - [01/Apr/2018:12:17:51 -0500] "GET /wp-admin/network/ HTTP/1.1" 302 453 "-" "Your User Agent"
[...]
XX.XX.XXX.XXX - - [01/Apr/2018:12:17:54 -0500] "GET /wp-admin/network/ HTTP/1.1" 302 453 "-" "Your User Agent"

There are a couple of pieces of information to note. Once you’ve familiarized yourself with what an apache access log should look like and it’s formatting, you’ll be able to see that the status codes are 302 (Found). Another thing to note is that there is one request with a size of object requested of 3779 bytes, followed by 20 requests with a size of object requested of 453. I’ve found that the size of the first request will change, but the size of the repeated request always seems to be the same. After 20 requests, you will get an error message for ERR_TOO_MANY_REDIRECTS and that will be that. Sound like your issue? Read on.

Here’s what worked out to solve the problem in my case. The primary blog has a BLOG_ID of 1 and a SITE_ID of 1 (this is common). Recall that in the wp-config.php file I’ve defined the DOMAIN_CURRENT_SITE as www.mysite.com. Using those IDs, I was then able to go to the database and check out the URL for that site, and…

mysql> select blog_id, site_id, domain 
    from prefix_blogs 
    where blog_id = 1 and site_id = 1;
+---------+---------+---------------------------+
| blog_id | site_id | domain                    |
+---------+---------+---------------------------+
|       1 |       1 | mysite.com                |
+---------+---------+---------------------------+
1 row in set (0.00 sec)

See the issue here? I’ve set the url to www.mysite.com in wp-config.php, but in the database it’s listed as mysite.com… with no www prefix. So with the DB and the config file disagreeing, the request just gets bounced back and forth until eventually (20 requests later) apache will give up. Add the database entry to www.mysite.com and everything works exactly as expected, with access to the Network Admin restored.

Hope this was able to help!

Share Your Thoughts

Leave a Reply