WordPress redirect loop – Dev Diaries #1

Welcome to my first ever dev diary!

I thought it would be a good idea to write my thoughts out through my work day and put them online. I quickly came to regret this decision (as you’ll see momentarily) but still I think it may give someone a good laugh so it’s going up anyway 😊

04/10/18 – Transferring a WordPress site from one server to another

Today I was transferring a website from one server to another and became stuck when all I could see was a redirect loop error when trying to access wp-admin…let’s begin.

Initial things I’ve tried initially

  • Cleared cookies…nothing
  • Cleared wp-content/cache/ directory, nothing
  • Set WP_cache to false
  • Renamed all plugins with a prepended dash
  • Deleted w3tc config folder
  • Tried updating siteurl and home url: added a trailing slash,

Digging deeper

Nothings worked so far, let’s try and get to the route of the problem and discover what’s actually going on:

  • index.php
  • wp-blog-header.php
  • wp-includes/template-loader.php

wp_redirect_admin_locations

After going down the above files adding a die statement periodically to check if a string would be output I found that the redirect loop was being caused by something hooked onto template_redirect. I fired up the project in visual code and used regex to find any usage of ‘template_redirect’: add_action\(\s?[‘”]template_redirect I used this to check for different variations of template_redirect this would catch, for example, add_action( ‘template_redirect and add_action( “template_redirect. There was quite a lot of usage in plugins but these had already been disabled so I wasn’t too worried about them, one was used in the currently active theme that had nothing to do with redirection so again wasn’t too worried about that. For WordPress core the template_redirect hook was used 7 times but I  saw one I wanted to look into first

add_action( ‘template_redirect’, ‘wp_redirect_admin_locations’, 1000 );

in wp-includes/default-filters.php I commented this line and saved the file went to my browser and refreshed and finally, finally I get a new error! Better still a PHP error!! Although I’m not out of the woods yet I’m relieved after seeing the redirect loop error for the last hour. Now it’s time to start looking into the wp_redirect_admin_locations function

wp_redirect_admin_locations can be found in wp-includes/canonical.php

So I’m currently confused, the wp_redirect_admin_locations function’s job is to ‘redirect a variety of shorthand urls to the admin’ and it seems to be working as it should but I don’t get it. It contains an array that holds relative urls that should be rewritten e.g /wp-admin, /dashboard and so on then checks if the request uri can be seen in that array and if it does redirect the user. This seems straight forward enough and I can completely get understand the purpose, if someone goes to domain.com/dashboard take them to the admin url. So why am I confused? Look at what I have:

$_SERVER[‘REQUEST_URI’] = /wp-admin (without trailing slash)

$admins = [‘/wp-admin’, …]

Admin_url() = domain.com/wp-admin

So it gets redirected to domain.com/wp-admin to then do the exact same thing again?!?!?

The redirect loop is obvious and I now see why I’m getting the error. But what is going wrong to cause this? And how did this happen in the first place? Transferring files and database from one server to another without changing the url shouldn’t cause this surely? I’m fairly sure I’m missing something blindingly obvious. Anyway moving on…I’m thinking that the url to wp-admin shouldn’t actually be wp-admin? My mind is melting, I know 100% that wp-admin is the right url and I can’t believe I’m doubting myself but why would redirection happen if the url contains wp-admin. Something is definitely going wrong, but what?

So after checking I wasn’t crazy and going to another site to attempt to access the dashboard I realised that when the user isn’t logged in they are redirected to wp-login.php then wp-admin. So I thought I’d try going directly to wp-login.php.

I saw a login form! But my happiness was quickly given the finger by this godforsaken site when it logged me in and took me directly to wp-admin which presented the familiar redirect error 😡

Googling gave me the idea to remove the wp_admin_redirect_locations action but this just (unsurprisingly) gave me my themes 404 page.

I’ve just added a die() statement to the wp_admin_redirect_locations function on a separate test default wp installation and it seems that this function doesn’t even run when access wp-admin!! Backed up a little to wp-includes/template-loader.php stuck a die at the top of that and again nothing when on admin pages. Upto wp-blog-header.php and another die statement and nothing on wp-admin

Wp-admin

And of course I realise, that we’re actually accessing the wp-admin/index.php on a working install….feeling slightly stupid right now.

Note: I can’t believe the penny still hadn’t dropped at this point

Moving on, this would indicate that wp-admin is in fact being treated as a standard blog post/page on our non working install which would suggest a problem with our .htaccess but I removed all the gubbins added by w3 cache and created a new .htaccess with just the default wordpress stuff found between the ‘begin wordpress’ ‘end wordpress’ comments.

So I remove everything from the .htaccess file and I get a 404 when accessing wp-admin.

The penny (finally) drops

Oh dear, I’ve just figured it out when I’ve made the transfer the wp-admin folder didn’t upload 😓 how stupid can you get…

I can only liken this to losing your keys and saying “I can’t find them” whilst staring right at them. Now that that ordeal is over it’s just a simple case of installing the ssl on the new server, updating the dns and testing everything is as it was before the transfer and done, granted this took way, way longer than expected but we can’t be perfect all the time! 😂

Return to Mr Mad Hat homepage