WordPress Apache PHP5-FPM and Redirect-Loop

Ok, here’s a quick and dirty .. just so I log this as it drove me crazy and I lost about 3 hours of my life on it.

Earlier in the day I wrote a script to sync configs across servers and that’s been working great.  Using a unison and some other bits, so keep this in mind that I’m syncing configs before reading…

Basically, after a WP upgrade, I noticed the homepage to the site was blank/white and looked like it was going to timeout.  Instead, it threw me into a redirect-loop.  Inspecting it, looked like it was 302 cached and looping until Chrome gave up.

Now I’m thinking that it had something to do with the config sync and I’m must have corrupted some php-fpm or apache config.

Things I checked.. php5-fpm config, apache2 configs, everything… I was starting to doubt myself whether or not php5-fpm was configured correctly and using sockets vs. TCP… ugh..

I noticed that I can manually execute php scripts, so that’s all good… php-fpm is running ok and connection to the backend database servers are good as well.  I can even get to the website if I added /index.php.  So that’s odd.. looks like my DirectoryIndex is not seeing the index.php.. So maybe a module didn’t load or something hosed with my .htaccess file, etc.. All checked out ok.

Then I noticed when I look at the headers for the redirection going on I see this:

HTTP/1.1 301 Moved Permanently
Date: Thu, 07 Jan 2016 03:49:58 GMT
Server: Apache
X-UA-Compatible: IE=EmulateIE10
X-Powered-By: W3 Total Cache/0.9.4.1
Location: http://yourdomain.com/
Vary: Accept-Encoding
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

0

But when I request it with /index.php I see this:

 

HTTP/1.1 200 OK
Date: Thu, 07 Jan 2016 03:50:59 GMT
Server: Apache
X-UA-Compatible: IE=EmulateIE10
X-Powered-By: W3 Total Cache/0.9.4.1
Link: <http://yourdomain.com/wp-json/>; rel="https://api.w.org/"
Link: <http://yourdomain.com/>; rel=shortlink
Vary: Accept-Encoding
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

2000
<!DOCTYPE html>
... blah blah... 
<link rel="pingback" href="//yourdomain.com/xmlrpc.php" />
... blah blah ...

Do you notice anything in the above links from WordPress?

WordPress is not pulling the site link correctly.  Notice it has no http: or https: in the above output.

The fix?  Even though the WordPress Address (URL) and Site Address (URL) are correct and includes the appropriate protocol, I re-typed it in both areas, saved it and all is good now!

If you can’t get into your WP Admin but are able to access the server, then you can either force it in the wp-config.php or your theme’s functions.php file.

If you edit your wp-config.php, you can force it for your URL here:

define('WP_HOME','http://yourdomain.com');
define('WP_SITEURL','http://yourdomain.com');

If you’re editing your functions.php file, you can force your URL at the top by adding this:

<?php
  update_option( 'siteurl', 'http://yourdomain.com' );
  update_option( 'home', 'http://yourdomain.com' );
?>
-- rest of your function code below this --

If you’re not able to access your website, or those files either via FTP or SSH, you’re kinda hosed.  But if you happen to have access to your database, look at your options table and make sure the home and siteurl are set accordingly:

mysql> select * from wpdb_options where option_name = 'home' or option_name = 'siteurl';
+-----------+-------------+----------------------------+----------+
| option_id | option_name | option_value | autoload |
+-----------+-------------+----------------------------+----------+
| 3 | home | http://yourdomain.com | yes |
| 1 | siteurl | http://yourdomain.com | yes |
+-----------+-------------+----------------------------+----------+
2 rows in set (0.00 sec)

mysql>

Make sure those are what they should be set to.  Hope this helps.