Page 1 of 1

Apache mod_rewrite Question

Posted: 2008-09-09 11:55am
by McC
Okay, so here's the skinny. In my top-level web root, I've got an .htaccess file that, among other things, has this in it:

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mcc3d.com$ [NC]
RewriteRule ^(.*)$ http://www.mcc3d.com/$1 [R,L]
The intent here is to redirect any request from "mcc3d.com/..." to "www.mcc3d.com/...". So far, so good. It works just fine.

Except...

...when I have another .htacess file, in a sub-directory. In this case, the top-level one is ignored in favor of the one in the sub-directory (which also uses mod_rewrite). This sub-directory holds a MediaWiki, and uses the following rule:

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?title=$1 [PT,QSA,L]
No problems there, either.

However, when I start trying to add the first mod_rewrite chunk to the second .htaccess file, it stops behaving as one might expect. The combined code I "expect" to work is:

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mcc3d.com$ [NC]
RewriteRule ^(.*)$ http://www.mcc3d.com/$1 [PT,R,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?title=$1 [PT,QSA,L]
But, using this brings up a "302 Found" page. What am I doing wrong?

Posted: 2008-09-09 09:26pm
by Alferd Packer
I would try the following:

Change

Code: Select all

RewriteRule ^(.*)$ http://www.mcc3d.com/$1 [PT,R,QSA] 
to

Code: Select all

RewriteRule ^(.*)$ http://www.mcc3d.com/$1 [PT,QSA] 
The R flag forces an external redirect, and if you don't specify an HTTP return code, it'll default to the 302 you're seeing. AFAIK, it's never going to get to the other conditions and rules because of this.

Posted: 2008-09-11 01:55pm
by McC
Thanks for replying, Alferd.

Unfortunately, that doesn't seem to work either, though it does create a different problem.

Code: Select all

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mcc3d.com$ [NC]
RewriteRule ^(.*)$ http://www.mcc3d.com/$1 [PT,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?title=$1 [PT,QSA,L]
This results in the following 404:

Code: Select all

http://mcc3d.com/[directory] --> http://www.mcc3d.com//home/[username]/public_html/[directory]
On a lark, I tried this:

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?title=$1 [PT,QSA]

RewriteCond %{HTTP_HOST} !^www.mcc3d.com$ [NC]
RewriteRule ^.*/([directory]/.*)$ http://www.mcc3d.com/$1 [PT,QSA,L]
And while it doesn't fail in the same way that the others do, it doesn't seem to actually work, either. The first RewriteRule functions as it normally did, but the latter never seems to activate. (Note: [directory] in the above pattern is actually replaced by the directory name in question)

Posted: 2008-09-11 09:43pm
by Alferd Packer
mod_rewrite is a strange beast indeed.

What about chaining the rules with the C flag? I've never done it myself, but hey, it can't hurt to try, right?

Another thing you might want to try (and this may not be applicable in your case) is using the RewriteRule directive in lieu of the RewriteCond directive? As I understand it, if you use a - instead of an actual URL, it does nothing, except for checking the pattern specified earlier. I've never done it this way myself, but the mod_rewrite docs have a couple examples.

An example of this would be:

Code: Select all

RewriteRule /someurl -
RewriteRule /part-of-someurl /otherurl
Again, as I understand it, you would use this to pattern-match multiple times; you first want to filter for patterns matching /someurl. Then, you want to check anything that makes it through that and has a subset of /someurl in it and redirect those, while treating all the rest as are. I dunno if it would help in your case, though.