views:

41

answers:

1

I have a number of domains pointed to the same webserver and would like to set it up to rewrite all incoming traffic to be under one consistent domain. I have done this before do make sure that the request has a www. in it, but when I added the script below to be site it started freezing up.

 RewriteCond %{HTTP_HOST} !^www\.domain\.com(.*) [NC]
 RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]

After I add this to my .htaccess (above all my other rewrites) and save the file to the server, the site will operate normally for around 10 seconds and after that requests to it will start freezing up. I know this is VERY weird, this happens AFTER the file is saved to the server. If I remove the lines above it will start working immediately.

I am a little lost as to what could be causing this strange problem.

UPDATE: I checked my rewrite logs comparing before and after the change was made. I see a lot of these error

 127.0.0.1 - - [21/Jul/2010:12:57:35 --0400] [280082-web1.dummy.com/sid#2b4899b49d30][rid#2b489a396148/initial] (2) [perdir /var/www/html/] rewrite '*' -> 'http://www.dummy.com/*'
 127.0.0.1 - - [21/Jul/2010:12:57:35 --0400] [280082-web1.dummy.com/sid#2b4899b49d30][rid#2b489a396148/initial] (2) [perdir /var/www/html/] explicitly forcing redirect with http://www.dummy.com/*

I also checked my access logs for the same timestamp and see this:

 127.0.0.1 - - [21/Jul/2010:12:57:35 -0400] "OPTIONS * HTTP/1.0" 301 333 "-" "Apache/2.2.3 (Red Hat) (internal dummy connection)"

I do have keepAlive turned on. Could this be causing the problem?

Also tried adding this cond to my rewrite, and it still errors: RewriteCond %{REMOTE_ADDR} !127.0.0.1

A: 

I'll start off by saying that this is speculation on my part (I'm not overly familiar with this aspect of Apache's processing), but it was a bit lengthy for a comment, and hopefully it'll help with investigating this a bit farther.

Since you're likely running a MPM like prefork, Apache's method of managing the child processes involves sending them a request to wake them up. I believe that this used to be a GET request, but given that requesting a real resource might put unnecessary strain on the server for this purpose, it seems that it now sends an OPTION request instead.

You also said that you have KeepAlive set to On. If your requests take a significant amount of time (as far as a typical request goes), it's possible that your child processes are tied up to the point where the server is required to spawn new ones to handle additional incoming traffic. Consequently, if your MaxSpareServers value is set low enough, once the processes free up, Apache may have a lot of children to kill.

To kill them, it needs to set that OPTION request in your log to make sure that they're awake. This ends up being handled by your mod_rewrite rule, which, when put together with everything else, might cause the extreme slow down that you're experiencing.

I'm not sure why your RewriteCond wouldn't work to prevent the redirect. You could try this, but I'm not sure why it would be any different:

RewriteCond %{REQUEST_METHOD} =OPTIONS [NC]
RewriteRule ^.*$ - [R=200,L]    

RewriteCond %{HTTP_HOST} !=www.example.com [NC]
RewriteRule ^.*$ http://www.example.com/$0 [R=301,L]
Tim Stone
Crap still freezes up the site. I am thinking I have to add a RewriteCond for each of the domains instead of wildcard.
Louis W
Damn. Hm, I don't fully follow, can you elaborate on what you mean?
Tim Stone
Like this:RewriteCond %{HTTP_HOST} =www.foo.com [NC]RewriteCond %{HTTP_HOST} =www.bar.com [OR,NC]RewriteRule ^.*$ http://www.domain.com/$0 [R=301,L]
Louis W