Apache cyclic redirection problem

apache-2.2redirectvirtualhost

I have an extremely weird problem with one of my sites. I run a number of blogs off a single apache2 server with a shared wordpress install. Each site has a www.domain.com main domain, but a ServerAlias of domain.com. This works fine for all the blogs except one, which instead of redirecting to www.domain.com redirects to domain.com, causing a cyclic redirection.

The configuration for each host looks like this:

<VirtualHost *:80>
    ServerName www.domain.com
    ServerAlias domain.com
    DocumentRoot "/home/www/www.domain.com"

    <Directory "/home/www/www.domain.com">
        Options MultiViews Indexes Includes FollowSymLinks ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

As this didn't work, I tried a mod_rewrite rule for it, which still didn't redirect correctly. The weird thing here is that if i rewrite it to redirect to any other domain it will redirect correctly, even to another subdomain. So a rewrite rule for domain.com that redirects to foo.domain.com works, but not to www.domain.com. In the same way, trying to redirec to www.domain.com/foo/ ends me up with a redirection to domain.com/foo/.

Even weirder, I tried setting up domain.com as a completely separate virtual host, and ran this php test script as index.php on it:

<?php
header('Location: http://www.domain.com/' . $_SERVER["request_uri"]);
?>

Hitting domain.com still redirects to domain.com! Checking out the headers sent to the server verifies that I get exactly the redirect URL I wanted, except with the "www." stripped. The same script works like a charm if I replace www. with foo or redirect to any other domain.

This has now foiled me for a long time. I've diffed the vhosts configs for a working domain and the faulty one, and the only difference is the domain name itself. I've diffed the .htaccess files for both sites, and the only difference is a path related to the sharing of wordpress installation for the blogs:

php_value include_path ".:/home/www/www.domain.com/local/:/home/www/www.domain.com/"

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I searched through everything in /etc (including apache conf) for the domain name of the faulty host and found nothing weird, searched through everything in /etc for one of the working ones to make sure it didn't differ, I even went so far to check on the DNS setup of two domains to make sure there wasn't anything strange going on.

Here's the response for the faulty one:

user@localhost dir $ wget -S domain.com
--2010-03-20 21:47:24--  http://domain.com/
Resolving domain.com... x.x.x.x
Connecting to domain.com|x.x.x.x|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 301 Moved Permanently
  Via: 1.1 ISA
  Connection: Keep-Alive
  Proxy-Connection: Keep-Alive
  Content-Length: 0
  Date: Sat, 20 Mar 2010 20:47:24 GMT
  Location: http://domain.com/
  Content-Type: text/html; charset=UTF-8
  Server: Apache
  X-Powered-By: PHP/5.2.10-pl0-gentoo
  X-Pingback: http://domain.com/xmlrpc.php
  Keep-Alive: timeout=15, max=100
Location: http://domain.com/ [following]

And a working one:

user@localhost dir $ wget -S domain.com
--2010-03-20 21:51:33--  http://domain.com/
Resolving domain.com... x.x.x.x
Connecting to domain.com|x.x.x.x|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 301 Moved Permanently
  Via: 1.1 ISA
  Connection: Keep-Alive
  Proxy-Connection: Keep-Alive
  Content-Length: 0
  Date: Sat, 20 Mar 2010 20:51:33 GMT
  Location: http://www.domain.com/
  Content-Type: text/html; charset=UTF-8
  Server: Apache
  X-Powered-By: PHP/5.2.10-pl0-gentoo
  X-Pingback: http://www.domain.com/xmlrpc.php
  Keep-Alive: timeout=15, max=100
Location: http://www.domain.com/ [following]

I'm stumped. I've had this problem for a long time, and I feel like I've tried everything. I can't see why the different domains would act differently under the same installation with the same settings.

Help 🙁

Best Answer

Awesome! A good answer was given by anonymouse in a comment to the post:

Have you tried opening the site directly from the host to rule out any funky dns issues? I.E., add an entry for www.domain.com and domain.com to /etc/hosts and then open the site with links from a command prompt. If you've setup a separate virtualhost (non wordpress site) and you're still getting redirected back to domain.com then i would be suspect of Via: 1.1 ISA in the http headers. A quick search reveals Microsoft Internet Security & Acceleration Server. A reverse proxy and caching server among other things. It could be causing problems

The problem was (well, is) the ISA server. I still have no idea why this happens since I don't run that server, but adding both hosts to /etc/hosts and locally doing a wget -S on it gave me the proper behavior.

I'll need to talk to the people running that server to see if they know what it's about, but at least that explains why I couldn't get any sense out of what was happening.

Edit: Fixed! Changing the "To" rule site name in the ISA server rule fixed it, even though link translation was off.

Related Topic