Wacky alien redirect issue solved!
by Sapphire (October 6, 2005)
If you don’t know why it’s important to establish your site links either with or without the “www”, using a 301 redirect to make sure the “wrong” version gets rerouted to the write one, read my article on 301 htaccess www redirects and come back to this for the rest.
Now, that’s all really simple and straightforward when you have just one folder of files. But what if you toss another folder under it with a php script that uses redirect rules to generate its dynamic pages? PHPBB forums, for example, behave perfectly, inheriting the rewrite rule from the main site flawlessly. But a lot of scripts don’t.
Take this site. I’ve got a Serendipity blog in the top folder, set to always redirect to the non-www version of my URLs. Under that is my directory, which is another script. It can’t function properly if it inherits Serendipity’s rewrite rules, because they clash. So I had to turn the rewrite off in the subfolder (directory), which meant that folks typing in http://www.bluemushrooms.com/directory/directory got the www version in their browser window, which is not good from a Google standpoint.
I found a solution for this after a LOT of trial and error and searching online. It’s almost embarrassingly simple, even though it sounds REALLY technical (bear with me, and ask questions if it’s confusing - I swear, if I can understand this stuff, anyone can!). And it should work between any two php scripts, one in a subfolder under the other, which have clashing rewrite rules. (I also had to use it for a directory under a Drupal CMS.)
So, using this domain for the example:
My main folder’s htaccess file (the Serendipity blog) has this rewrite rule to keep people on the non-www version of my URL’s:[quote]RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^bluemushrooms\.com$
RewriteRule (.*) http://bluemushrooms.com/$1 [R=301,L][/quote]
But as I stated above, I had to turn off the main folder’s redirect rules in the subfolder’s htaccess. And that meant people typing in “www.bluemushrooms/directory” would get the www version, which is not a Google happy situation.
Here’s the solution. This is the htaccess in my subfolder that makes it work. Notes in bold explain step by step what’s being accomplished:
RewriteEngine Off (turns off S9’s clashing rewrite rules)
#lmasetting (these are the settings the subfolder script needs to function - your script will probably have its own rules, or it may not have any)
ForceType application/x-httpd-php
#lmasettingend
RewriteEngine On (turns rewrite back on for subfolder)
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^bluemushrooms\.com$
RewriteRule (.*) http://bluemushrooms.com/directory/$1 [R=301,L]
(this is the key line that tells it where to redirect people who use “www”. By sticking my subfolder at the end (bold), each folder gets the appropriate redirection to work the way I want)


July 5th, 2007 at 3:34 pm
[...] as easy as all that, or so I thought… The first bit of help I discovered was at the Affiliate Marketing Journal, and he had the right idea about turning OFF the rewrite engine and then turning it back on (in a [...]