Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to force users on my site to use the www version, however my configuration is causing an infinite redirect loop. The relevant configuration is below:

<VirtualHost *:80>
  ServerName  www.mydomain.com

  DirectoryIndex index.html index.php
  DocumentRoot /home/me/sites/mydomain.com/htdocs

  # Log file locations
  LogLevel warn
  ErrorLog  /home/me/sites/mydomain.com/logs/error.log
  CustomLog /home/me/sites/mydomain.com/logs/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName mydomain.com
    RedirectMatch permanent ^/(.*) http://www.mydomain.com/$1
</VirtualHost>

This method is the way I've found to be the most acceptable from answers on Stack Overflow.

However, when browsing to www.mydomain.com or mydomain.com the following happens:

http://i.imgur.com/1dcEY5x.png

As you can see, it's bouncing me between www and non-www until the browser quits.

I've verified that it's not a .htaccess issue by completely removing it from my document root, yet it still happens.

Any ideas?

share|improve this question

3 Answers 3

up vote 0 down vote accepted

You definitely have another redirect somewhere.

Just to be sure, I set up your redirect locally:

<VirtualHost *:80>
  ServerName  www.ddddd.com
  DirectoryIndex index.html index.php
  DocumentRoot E:/temp
    <Directory "E:/temp">
        Order Allow,Deny
        Allow from 127.0.0.1
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName ddddd.com
    RedirectMatch permanent ^/(.*) http://www.ddddd.com/$1
</VirtualHost>

hosts:

127.0.0.1           www.ddddd.com
127.0.0.1           ddddd.com

Redirecting back to the same server, it works perfectly. Since I can't see your actual domain, I can't look at your DNS situation, but if you're absolutely sure you have no other redirects on the www server, that's where I'd look: dig those 2 domains. www might not be where you think it is, or DNS might be pointing all subdomains at the root domain.

Another possibility is that your www.mydomain.com/index.php contains something like the following.

<?php
Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://mydomain.com" ); 
?>
share|improve this answer
    
Turns out it was Wordpress... I had the non-www version specified in my options and Wordpress was redirecting back to non-www. Thanks! :) –  Chris Aug 25 '13 at 22:16

Looks to me like that rule is redirecting everything to www (including www.mydomain.com).
Here is the redirect rule I'm using in my .htaccess to achieve www redirect:

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

Note that this will redirect all subdomains to www, so you may want to use

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

to only redirect calls without an explicit subdomain.

share|improve this answer
1  
That rule is redirecting everything, but it's only present in the virtual host applying to the non-www domain - requests other than those are not affected. –  Shane Madden Aug 25 '13 at 17:46

Your Apache configuration is not responsible for this - the application code running within Apache is redirecting from www to the non-www domain.

This situation could normally be assumed when redirects are happening that can't be explained by the Apache config, but the different size and much slower response time for the redirect that the application is generating lends further weight to it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.