Resolving The WordPress Multisite Redirect Loop

Though I do the majority of my work using single site WordPress installs, there are a number of sites and projects in which I’ve used WordPress multisite and there’s a problem that I’ve experienced specifically with using WordPress multisite, subdomains, and shared hosting environments.

Specifically, the problem is this:

  • Install WordPress and activate multisite
  • Configure the installation to use subdomains (versus subdirectories)
  • Attempt to login and get stuck in a redirect loop

If you have a single instance of WordPress multisite installed on the same server, there’s no issue, but if you go beyond that then you normally hit a problem: a redirect loop.

The WordPress Multisite Redirect Loop

The WordPress Login Screen

The most frustrating screen ever (in a redirect loop, that is).

Once you’ve increased the number of your multisite installs beyond one, then you’re likely to be unable to login as you’ll get stuck in a redirect loop. That is, every time you try to login, you’re returned to the login screen.

Luckily, the fix is relatively easy.

In your wp-config.php file, add the following lines of code:

define('ADMIN_COOKIE_PATH', '/');
define('COOKIE_DOMAIN', '');
define('COOKIEPATH', '');
define('SITECOOKIEPATH', '');

And do so just before the line that reads:

/* That's all, stop editing! Happy blogging. */

Once done, the redirect issue should be resolved.

Why Does This Happen?

Whenever you’re running multiple versions of WordPress on the same server, you can visualize the setup like this:

WordPress Multisite Installation

WordPress Multisite Installation

Basically, each version of WordPress, regardless of its domain or subdomain, maps to a single IP address. In this case, 192.168.0.1.

When a request comes into the server, part of the request includes the domain. A domain is associated with an IP address. When a cookie is created, it includes the name, some sensitive content, and then the path.

For example:

NAME = wordpress_d676ec21cf050e966685794aa715694f
CONTENT = removed
PATH = /sitename/wp-admin

In a WordPress Multisite setup a cookie for two sites may look like this:

NAME = wordpress_d676ec21cf050e966685794aa715694f
PATH = /sitename/wp-admin

NAME = wordpress_d676ec21cf050e966685794aa715694f
PATH = /sitename/

Notice that the name of the two cookies above are exactly the same but the path’s are different. This is because two different sites with different domains are hosted on the same IP address, and they both exist in the cookie because the cookies aren’t being reset.

Cookies Being Set By WordPress

Cookies being set for the different sites on the same domain.

As such, when you attempt to login to a WordPress installation on a different domain (but on the same IP), the cookie is essentially invalid.

Thus, WordPress – in the most technical term possible – wigs out.

But more seriously, wp-login doesn’t attempt to look for cookies before actually setting them. This means that an invalid cookie is being used and since it doesn’t attempt to clear the existing cookies, you get stuck in the login loop.

Thus, the big picture looks something like this:

WordPress Multisite and Cookies

WordPress Multisite and Cookies

Sure, clearing the cookies will do the trick, but users shouldn’t have to do that. Additionally, not everyone will see this problem occur, but if you’re in the business of managing a multisite installation in a shared environment, then you’re likely to see it.

The code above will ensure that WordPress is clearing the cookie for the given domain of the multisite thus allowing the login process to set it correctly.

115 Comments

This snippet of code worked like a dream, so thank you. Odd that I had the problem a day after you posted the solution too.

Thanks so much for posting this, I’m anxious to try it on an issue I’ve been having after a Multisite migration attempt. I’ve migrated a Multisite install to another domain for dev-ing and am getting this redirect.

Hopefully this will solve the issue for me:)

    Sure thing – if it doesn’t work, let me know. This particular issue is one that obviously hits close to home so being able to provide as many solutions to it as possible would be great.

      I tried to solve this issue with the solution you provided but it still doesnt work. Any other thing to try ?

      Thanks in advance.

      Alex

      Hi Tom,
      I am having this issue with the new install of WP 3.6.
      I installed fresh, activated multi-site, and created a new site. I am woring with sub-folders. Now it is giving me the redirect error:

      The page isn’t redirecting properly

      Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

      This problem can sometimes be caused by disabling or refusing to accept
      cookies.

      I tried using the code you suggested and it isn’t working. I’m thinking it’s because you were working with sub-domains instead of the sub-folders.
      Do you have any suggestions? It has been awhile since you posted on this, so I thought you may have figured something out.
      Thank You!

        Unfortunately, I’ve got nothing in addition to the content mentioned here as this is what’s ended up solving my issue.

        If you end up solving it on your end, feel free to leave it in the comment stream here just in case it may help others :).

Worked like a charm! I was banging my head against my keyboard trying to figure out what was making it loop. And, oddly, I just installed with just one site.

I had similar problem with a sub-directory WPMS setup. mine would come up with a browser error of too many redirects. I had already tried clearing cookies and cache.

This fix solved it! Thank you!

I also had a cookie problem on a subdirectory install of multisite. One point to mention and i’m not sure why this occurred but WP didn’t like this line:

define(‘COOKIE_DOMAIN’, ”);

Once i removed that line from wp-config.php it seemed to still fix the problem.

Any thoughts?

THANKS A LOT you solved my 1hr-headache.
Peace and love to you and your family.

I also have a loop in wp-admin section. I’ve added your code, but it still have no effect: the login redirect loop is here.

Thanks that really helped! I don’t really follow why this is a problem on local and not on a live server, which will also have the same IP for each site?

Secondly, as I’d like to maintain one set of code that works on live and local, can the 4 paths exist in the LIVE config file, or will this break logins there?

    Thanks that really helped! I don’t really follow why this is a problem on local and not on a live server, which will also have the same IP for each site?

    That is odd as I normally see it the other way around :). I was going to double-check about the IP issue on the server, but if it’s not a problem there then it may simply be a result of the routing mechanism setup on the server (unless you’re on a run-of-the-mill host).

    Secondly, as I’d like to maintain one set of code that works on live and local, can the 4 paths exist in the LIVE config file, or will this break logins there?

    The most critical paths are in the database, so if you have different paths for your siteurl and home values in the wp_options table, then you’re out of luck. =T

Greetngs Tom, and thanks for all the information which has been provided above.

We have applied the changes above, but it has yet not resolved our error. For we have a setup such that blog.domain1.com and blog.domain2.com. The login for blog.domain1.com works correctly, but the login for blog.domain2.com gives an error.

We have tested, the configuration for both the blogs from the admin console, we have tested all the php files which are the same except for a few obvious changes.

Any inputs or pointers will be appreciated.

    Hey Saurab,

    Unfortunately, there may not be much more can I offer. What error does the login for blog.domain2.com give? Make sure that that user has been granted permission in the main multisite admin panel – sometimes that can cause an error.

      Hi, I should have stated it more correclty. My mistake. The login on blog.domain2.com does not give an error but redirects to the /wp-login.php without any error as such. No message on the screen. Just verified that the user has been added to blog.domain2.com and has adequate permissions.

      One more funny thing which I observed, is if i go to the main blog and navigate to my list of blogs, and click on the blog, it opens the blog blog.domain2.com for me. Then I type /wp-admin at the end as in blog.domain2.com/wp-admin and press enter. I am taken to the dashboard.

      Thanks and appreciate any help you can give me. Frankly can use any help I get.

        Unfortunately, this sounds like more complicated of a problem that I can provide via blog comments, but I’d double-check your .htaccess file, your wp-config.php file, clear your cookies, and disable any caching plugins if you have them.

        I’d also recommend trying to login with another browser and see if the problem persists there, too. If it does, then the problem definitely exists on the server so that’s the next place I’d start looking.

Thanks so much. I’ve spent days looking for this problem. You fixed it

A BIG thank you to you!!! wordpress.org forums have 2-3 years old discussions about this with no real solution. I had to google this and visit an external site, wow!

Thank you for documenting the solution to a very difficult problem. It worked for me, twice!

Your solution worked until I installed the domain mapping plugin “wordpress-mu-domain-mapping”. It required me to comment out the second line:
define('COOKIE_DOMAIN', '');

I didn’t know it at the time because I was still logged in but that inevitably broke my setup because after a reboot, I could not login. The redirect loop was back! My sites were all rendering when visited by the subdomain url but I could not login to any of them.

I came back to your post and tried to remove the comment the COOKIE_DOMAIN line and then everything broke. The solution I came up with was to remove the network activation of the “wordpress-mu-domain-mapping” plugin using phpMyAdmin. Once the plugin was removed, sunrise.php removed from wp-config.php and the COOKIE_DOMAIN line back in action, I was in business. It worked.

My head is spinning after editing…

wp-config.php in WordPress
hosts file on Windows 7
httpd.conf in the WAMP Apache config
httpd-vhosts.conf in the WAMP Apache config
and .htacess in the virtual host setup…

it’s quite complicated.

Thanks again for your continued dedication to the WordPress community.

    Thanks so much for sharing what you ultimately had to do to get it to work – each environment can be tricky and this helps to provide a good solution for others who stumble across the same problem :).

    I had the same problem. It existed prior to installing the MU Domain Mapping Plugin.

    Adding these did fix the problem.

    1 define(‘ADMIN_COOKIE_PATH’, ‘/’);
    2 define(‘COOKIE_DOMAIN’, ”);
    3 define(‘COOKIEPATH’, ”);
    4 define(‘SITECOOKIEPATH’, ”);

    but the 2nd line “define(‘COOKIE_DOMAIN’, ”);” is incompatible with the MU domain mapping plugin. It makes you take out the line, and soon as I do, the login loop problem comes back. What does this mean? Is the plugin basically crippled at this point?

    My setup is:
    a clean fresh install of WordPress 3.5.2 and
    Version 0.5.4.3 of MU Domain Mapping.

      Unfortunately, I can’t offer much advice here as I don’t use the domain mapping plugin – I’ve just made those modifications to my `wp-config.php` (in a multisite install) and it was all I needed.

      Wish I could be more help.

        Yeah setting COOKIE_PATH to empty string can cause issues with plugins that use cookies.
        I had issues with the Easy digital downloads cart after doing this.
        setting it to forward slash solved it though

This was a very helpful post. I found that once I included this in wp-config.php, I could finally log into wp-admin. Once I got in, though, it was forcing me to log in again to each site when switching from dashboard to dashboard. So once I had logged into each dashboard, I found that I could remove these constants and everything started working normally.

Hi there,

i recently asked my developer to transfer all 7 of my wordpress websites into one multisite.

The main site is http://www.moltensky.com which is like a ‘quick view’ portfolio site.

I then have different sites for different products linking from this site for example http://www.moltenskyweddings.com.

My developer has set up the domain as http://www.weddings.moltensky.com that seem to redirect to moltenskyweddings.com.

I’m not sure, but I believe that Google does not like re-directs for SEO etc so I was wondering if it it possible to set up multisite without using http://www.weddings.moltensky.com.

Any feedback or advice would be greatly appreciated.

Many thanks,

Tommy (:

I implemented the code but I still have the redirect problem. The domain in question is not the main domain of my hosting account. I mean, I purchased the hosting service with “domain1.com”. Several months after, I setup the “domain2.com” as an add on domain via cpanel. This is the domain that is causing me the redirect loop when I create the new multisite “site” with the address “domain2.com/subsite”

Do you think that being “domain2.com” an add on domain in my main account is causing this solution not to work?

HELP !
I set up an NGINX+Wordpress+W3 thingy on AWS free tier and then activated multisite (sub directories, no sub domains). Then while trying to access the newly created site’s dashboard, got stuck in the redirect loop. Also the new site looks horrible. Implemented your fix. Didn’t work. Configured the NGINX config file to chew out the www part and used rewrite ^(.*) http://example.com$1 permanent; to sort out any non-www vs www problems for multisite. Also in wp-config, just left the define(‘MULTISITE’, ‘true’); part and commented out the extra load given by WP while setting up multisite to avoid confusion with www and non-www. All these failed. Need help :(

    Hey mk,

    Unfortunately, there’s not much more support than I can offer than what’s included in the post above.

    If I were in your position, I’d start back from ground zero and completely turn off multisite, clear your cookies, and follow each step one-by-one.

    Other than that, that’s the best I can do =T.

Oh my it works, really great snippet, Thanks alot!!

Thanks so much! I spend hours on trying to solve this and read about a dozen other forums threads trying to find a solution, but nothing worked. Your solution finally does.

Just one thing I want to add. I also tried clearing the cookies, cache, using a different browser etc., but I was still getting the loop. So I’m happy that is does work, but your explanation why doesn’t make sense to me in this case.

Thanks again!

thanks for this Tom, it helped! Even though I don’t really get the explanation :)

This problem plagued me for hours. I’m so glad I stumbled on this page. The code worked like a charm!

Thank you, thank you, thank you! Oh, it feels so good to finally be able to log into my site again! Bless you for supplying this code.

Thanks for this. Resolved our issues with MU WordPress 3.6 running on Ubuntu12.

Hi ! i have some time dealing with this problem, and i can’t fix it yet, i try your solution, but it’s not work for me, do you have another advice, or some point that i’ve missed ?

thanks for share !

I was also having this issue and I couldnt fix it with the solution displayed here. Anyway, I checked the php log file and I saw it was a problem with two specific tables not being created in the wordpress install: wp_blogs and wp_sitemeta. It seems like wordpress MultiSite checks these two tables on loading.

This may seem far from being the real solution to this problem but it was my specific case. Thanks.-

Hey tom, i am facing same problem, but the solution provided didnt worked for me. May be one of the reason could be that i forgot to add blog.dir folder in my wp-content at the time of enabling multi site

Hi there,

I have similar problem with my WP multisite install under Nginx box.

every I visited my domain, it always redirected to the signup page with link look likes http://domain.com/wp-signup.php?new=quietrm.com

I have tried your solution by modifiying wp-config.php, but it returned this message, every I visit my domian

The constant “COOKIE_DOMAIN” is defined (probably in wp-config.php). Please remove or comment out that define() line.

I’d appreciate anyone who can help me resolve this issue

Thanks for this, Tom! The wp-config.php code did not help my problem (though, I’m glad I know about it now) but clearing my cookies did. Just thought I’d put that into the conversation.

Adding to reports that these steps broke my WPMU setup… resulted in a white screen with:

“The constant “COOKIE_DOMAIN” is defined (probably in wp-config.php). Please remove or comment out that define() line.”

Removing that line, so it just says
define(‘ADMIN_COOKIE_PATH’, ‘/’);
define(‘COOKIEPATH’, ”);
define(‘SITECOOKIEPATH’, ”);

works great.

Thanks so much Tom

Sorry Tom, cancel that last comment – redirect loop still there on some sites. it seems the line that clashes cannot just be removed.

Tom! Please excuse my previous emails. But I believe I have a solution.

Domain mapping is case sensitive. So

myname.com will work, but MyName.com will have a redirect loop.

I hope this helps someone, and thanks for your work on this issue.

Thank you so much. really feeling relax now. Thanks again.

Tom, thanks so much. Great fix. One thing to note. With WordPress 3.8 this issue doesn’t occur with subdomains (foo.example.com and bar.example.com) because wordpress sets the cookie-domain to “.example.com” enabling single signon between the two. But it sure as heck does happen when you have 2 sites in a mult-site wordpress installation like http://www.foo.com and http://www.bar.com

Thanks again

    Thanks so much for noting that it’s not an issue with WordPress 3.8 – well, at least not in some cases ;) – this will be good for anyone else who stumbles across this post.

LOL, I actually found your article exactly before I encountered that bug. Thanks so much for the solution!

Thanks for this Tom. It worked perfectly! =)

Tom ? You saved my life ! hahahahahaha :D

Legend! Had this problem for just 2 days but has been driving me insane, such a simple fix – thank you!

Thanks for posting this but sorry to say it didn’t work for me.

What did work was replacing the rewrite code foor the .htaccess that was generated during the network set up with the code given in the wordpress codex site found at the following link:

http://codex.wordpress.org/Multisite_Network_Administration#.htaccess_and_Mod_Rewrite

This worked immediately :)

    Thanks for posting your solution – it’s great to have for reference for others who experience the same issue.

    Ditto. I used the “Fubfolder Example WordPress 3.0 through 3.4.2″ htacess template on my 3.9.1 install (has been upgraded through versions and not sure what about that means you have to use different htaccess rules)…

    … but anyway those htaccess rules fixed a similar redirect loop issue for me on a subfolder multisite setup.

      Thank you Jason!

      Your post helped me resolve the same issue today because it was not clear (to me) what version of the .htaccess file I should use. I knew I was working with an older WordPress install, but I was not certain how old.
      Now running the current WordPress Version 3.9.1 and I recently converted it to multisite with subfolders and got stuck in the “too many redirects” loop with the first sub-site I installed. It required the .htaccess file for “WordPress 3.0 through 3.4.2″ and that did the trick.

      Thanks to Tom for originating this useful topic. The discussion is still helpful over a 18 months later. -Cheers

        Glad to have been able to have offered help, Michael! And glad to have others commenting on this issue and contributing their fixes :).

Thanks! I was setting up multisite for a Magento integration, and was near pulling my hair out as I had everything set up correct, but kept getting this login redirect loop. The code worked like a charm even with no modifications necessary. Running multisite in a subfolder install under magento, so that’s two htaccess files to consider. Thanks again!

works great, thanks a lot

Ran into this today with a client site. He installed a security plugin which added:
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

to his WP-CONFIG.PHP file caused a loop since he didn’t have a cert installed for his domain.

Also, on my own multisite install I had a loop issue until I fixed the permissions/ownership on .htaccess as I created it when logged in as ROOT and forgot to fix it when I was done.

I LOVE YOU!!! Thank you thank you thank you!!!

Brilliant. Thank you so much! Those 4 lines of code solved my problem. (And thanks for defining very clearly why it happens too. That was interesting to learn.

Thank you SO much!! I just saw this error on my blog and freaked out, and this fixed it in about 30 seconds.

has anyone ever told you that you are awesome.

Cheers Tom, if only i found this blog over 2 hours ago and 3 re-installs earlier.

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs. But he’s tryiong none
the less. I’ve been using Movable-type on numerous websites for about a year and
am worried about switching to another platform.
I have heard fantastic things about blogengine.net. Is there a way I
can import all my wordpress posts into it? Any kind of help would be really appreciated!

    My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he’s tryiong none the less.

    When you’re talking about the costs, are you talking about the costs that you’d incur moving something from one architecture to another (that is, paying someone to port an application)?

    I mean, the licensing for .NET for commercial products is pretty expensive itself so there’s one argument against it; however, I used to do .NET, too and was happy with the environment so I’m playing devil’s advocate more than anything else.

    Is there a way I can import all my wordpress posts into it? Any kind of help would be really appreciated!

    Unfortunately, I don’t know about moving from WordPress to another system as I’m 100% focused on WordPress these days.

Thanks that solved my problem!

Thanks man, this worked like magic for my website. Keep up the good work :)

Also ran into this issue, added in the supplied code but the redirect loop kept occurring. Managed to get it fixed in the end though!

I read elsewhere that ‘localhost:80′ wont work WP multi-site, so I tossed a domain in my hosts file (let’s say derps.com) to point to 127.0.0.1

After that, I went through all of the WP database tables, replaced any “localhost:80″ occurrences I found with “derps.com”. Then update define(‘DOMAIN_CURRENT_SITE’, ‘derps.com:80′) in wp-config.php, restart my webserver… and voila.

The above probably only applies to rookies like me that were dealing with localhost and just kind of derping along.

Solution for my problem with WPMU Domain Mapping without using the COOKIE_DOMAIN fix in the UK at least.

Watch out for incompatible domain names basically… Try a traditional format one. e.g domain.org.uk instead of domain.uk

Some browsers (Google) appear to be rejecting new style domains it seems confusing them with TLD’s I think.

See
http://wordpress.org/support/topic/cookies-domain-mapping-loginlogout-problem?replies=12#post-5740914

Tom,
Thank you, Thank you, Thank you! I have been playing with this for a week. I gave up, went back to single site and decided to give it one more go. I wouldn’t have been able to have a multi site network without your expertise so I sincerely thank you.

Gracias por esta informacion!

Solved!! Thx for this greating post!

Leave a Reply

Name and email address are required. Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>