Forum Moderators: phranque

Message Too Old, No Replies

Apache virtual hosts & rewrite rules - redirect loop

         

chaosas

6:44 pm on May 28, 2008 (gmt 0)

10+ Year Member



Hello,
I've been trying to solve this problem for a week with zero success. I remembered I've been registered here for ages so here it goes. I hope I'm posting in the right place (I'm running apache2).

I have a VPS and several domains that are pointed to said VPS with A records. Everything is pointed there including all subdomains (using a wildcard I believe - the domain control panel is rather simple). Since it also points ALL subdomains to the VPS and shows them as a mirror of main site, I have created virtual hosts and rewrite rules for them which redirects all subdomains (including www) to the main domain.

Here's how it looks (info made up):

<VirtualHost 77.77.77.77:80>
ServerName mydomain.com
ServerAlias mydomain.com *.mydomain.com
DocumentRoot /var/www/html/mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^\mydomain\.com
RewriteRule ^/(.*)$ http://mydomain.com/$1 [R=301,L]
</VirtualHost>

Here's where it goes bad. I have three domains parked at the VPS using configurations similar to above. All are pointed to the VPS in the same way from the same domain control panel. They work OK (open normally, all subdomains are redirected to the main one with a 301, etc).

The problem is just ONE domain which when using the above configuration gives me a "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." error.

Basically if I open it it redirects to ITSELF with a 301 (mydomain.com > mydomain.com > mydomain.com...) loop.

If I open a subdomain it redirects correctly at first, but then loops (anysubdomain.mydomain.com > mydomain.com > mydomain.com...).

If I replace the domain to another one (and park it to the same location with same settings) it WORKS.
If I disable the Rewrite rules for this domain it WORKS (but the subdomains don't redirect as I want of course).

What is the difference between this domain and others that causes this problem? Beats me. The only thing I can thing of is that when I was buying the VPS I set mydomain.com as the intended domain. As a result, the hostname of the server is server.mydomain.com (I am unable to change it; it changes back after reboot).

Can that cause the said problem? All other settings are 100% identical as far as I can tell and simply changing the domain to another one works.

Any suggestions please?

jdMorgan

7:14 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the setting of UseCanonicalName? -- If "On", try turning it Off.

Also, note that you've got an unnecessary backslash following the start anchor on the RewriteCond pattern above. And you may wish to end-anchot that pattern as well, to redirect "mydomain.com.", "mydomain.com:80", or "mydomain.:80" -- all of which are valid, but non-canonical.

Jim

Jim

chaosas

7:37 pm on May 28, 2008 (gmt 0)

10+ Year Member



Wow, I don't know what to say. Thank you so much. I got fixed it right away by changing the rewrite code like you said:

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

(UseCanonicalNames was already off BTW).