Drupal 7 subfolder forwarding error in the .htaccess file

3 years 11 months ago

This was an odd problem.  All of the sites on our server are in sub folders.  So this site is actually cowleyworks.com/macsaveny.  The problem seemed to occur out of nowhere, but my guess is GoDaddy updated apache in some form.  So at around 3:00 half of my sites lost their background styles.

That was only the visible symptom.  They also had some funky redirects in some of their links.  Some would work fine while others were adding theier install folders to the urls and coming up with an error page.  Ex: A link  that should go to macsaveny.com/code was going to macsaveny.com/macsaveny/code.

After a few stop gaps and htaccess hacks I figured out the culprit.  It was this code in the basic drupal 7 install of htaccess:

# To redirect all users to access the site WITH the 'www.' prefix
# uncomment the following:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This explains why it was happening on half my sites.  For some reason I chose to do www on some and no www on others.  The non www's still had problems but the symptoms were less visable.  So, the solution:

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

I went through and made all my sites use this instead and they stopped having the folder redirect problem.  Something with the {REQUEST_URI} was screwing it up.  I have no idea why.... try as I might I cant understand htaccess code.  Hopefully this helps someone out there.  There must be another developer with the same circumstances.