We're trying to setup Apache on a public server to proxy requests to Apache with WordPress on another server, but we're getting infinite redirect loops.
Requests starting with www.example.com/about should get proxied to the internal server wp.example.corp. e.g.
www.example.com/about/about/team => proxy => wp.example.corp/about/team
To further complicate matters, our WordPress installation is in a subdirectory called "wp". So, on the WordPress server, we have the following settings:
- WordPress Address: http://www.example.com/about/wp
- Site Address: http://www.example.com/about
Here are some of the relevant bits from the Apache config on the public server:
<VirtualHost *:80>
#...
ProxyPreserveHost On
ProxyRequests off
ProxyErrorOverride On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
RewriteCond %{REQUEST_URI} ^/about/?$
RewriteRule ^/about/?$ http://wp.example.corp/ [P,L]
RewriteCond %{REQUEST_URI} ^/about/.+
RewriteRule ^/about/(.*) http://wp.example.corp/$1 [P,L]
#...
</VirtualHost>
The Apache config on the WordPress server is pretty straightforward:
<VirtualHost *:80>
#...
DocumentRoot /var/lib/publicpages
<Directory /var/lib/publicpages/>
Options Indexes FollowSymLinks MultiViews
AllowOverride FileInfo Indexes Limit Options
</Directory>
</VirtualHost>
And here is the .htaccess file from the root of the WordPress server:
# 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
With LogLevel set to debug, I can see the requests hitting the public server and responding with 301 redirects:
x.x.x.x, y.y.y.y (y.y.y.y) - - [25/Jan/2012:16:44:28 -0600] "GET /about/about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"
x.x.x.x, y.y.y.y (y.y.y.y) - - [25/Jan/2012:16:44:28 -0600] "GET /about/about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"
And I can see the same requests hitting the WordPress server, who is responding with 301 redirects:
z.z.z.z - - [25/Jan/2012:16:44:28 -0600] "GET /about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"
z.z.z.z - - [25/Jan/2012:16:44:29 -0600] "GET /about/team/ HTTP/1.1" 301 20 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2"
I also enabled full Rewrite logging on the WordPress server and see the following repeated over and over again:
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] add path info postfix: /var/lib/publicpages/about -> /var/lib/publicpages/about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] strip per-dir prefix: /var/lib/publicpages/about/team -> about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] applying pattern '^index\.php$' to uri 'about/team'
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] add path info postfix: /var/lib/publicpages/about -> /var/lib/publicpages/about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] strip per-dir prefix: /var/lib/publicpages/about/team -> about/team
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] applying pattern '.' to uri 'about/team'
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (4) [perdir /var/lib/publicpages/] RewriteCond: input='/var/lib/publicpages/about' pattern='!-f' => matched
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (4) [perdir /var/lib/publicpages/] RewriteCond: input='/var/lib/publicpages/about' pattern='!-d' => matched
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (2) [perdir /var/lib/publicpages/] rewrite 'about/team' -> 'index.php'
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (3) [perdir /var/lib/publicpages/] add per-dir prefix: index.php -> /var/lib/publicpages/index.php
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (2) [perdir /var/lib/publicpages/] trying to replace prefix /var/lib/publicpages/ with /
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (5) strip matching prefix: /var/lib/publicpages/index.php -> index.php
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (4) add subst prefix: index.php -> /index.php
x.x.x.x - - [26/Jan/2012:09:54:04 --0600] [www.example.com/sid#80e06c20][rid#80fa4200/initial] (1) [perdir /var/lib/publicpages/] internal redirect with /index.php [INTERNAL REDIRECT]
What am I missing?
We gave up and just made the URLs/paths match in both Apache servers. E.g.
While that doesn't exactly fix the issue, it effectively sidesteps the problem and eliminates the redundant 301 redirects.
You could use ProxyPass to do this, instead of using a Rewrite rule.