Can you help me with a mod_rewrite problem...?
July 8, 2009 9:33 AM   Subscribe

Can you help me with a mod_rewrite problem...?

I have two servers with copies of the same code on them. Lets call them www.example.com and test.example.com

on www.example.com the files are in the directory /var/www/html/thesite

on test.example.com the files are in the directory /var/www/html/stupidteststructure/anotherdir/thesite_test

Sadly, I can't do anything about this.

Now, on the test server I have the following .htaccess file in the /stupidteststructure/thesite_test directory...
RewriteRule ^$                        /stupidteststructure/thesite_test/content/  [R]
RewriteRule ^content$                 /stupidteststructure/thesite_test/content/  [R]
RewriteRule ^content/$                /stupidteststructure/thesite_test/process.php?page_alias=index  [L,QSA]
RewriteRule ^content/(.*?)([/]*)$     /stupidteststructure/thesite_test/process.php?page_alias=$1  [L,QSA]
It could probably be improved, but it works and everyone is happy.

Now, the problem is, when the code is copied to the live server, including the htaccess file, it all breaks because /stupidteststructure/thesite_test doesn't exist on that machine.

Is there a good simple way to make this more generic, or am I going to have to add conditional rewrites for the two servers?

I'm hoping there is something simple like the following, where RTTD is some super sekrit "Relative to this directory" flag...
RewriteRule ^$                        content/  [RTTD,R]
RewriteRule ^content$                 content/  [RTTD,R]
RewriteRule ^content/$                process.php?page_alias=index  [RTTD,L,QSA]
RewriteRule ^content/(.*?)([/]*)$     process.php?page_alias=$1  [RTTD,L,QSA]
Help? I'm supposed to have gone home half an hour ago... :)
posted by twine42 to Computers & Internet (6 answers total)
 
You can do a RewriteRule to populate a variable based on another test -- so, based on the server's hostname you could fill in a var for either the /stupidserverstructure/ string for dev, or blank for prod.

Look at the E= flag. If this isn't enough, and no-one else points you the answer, I'll have a poke at it later and try to generate a full receipe.
posted by devbrain at 9:38 AM on July 8, 2009


Best answer: Ok, here's a recipe that does it. I would be hesitant to put this into production, but it demonstrates how it might be done. (I would rather adapt my file promotion methods than have dev config get pushed into the production server).


RewriteEngine On

RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*) - [E=qPRE:stupidteststructure/thesite_test/]

# Replace these with your real rules ....
RewriteRule ^$ /%{ENV:qPRE}foo [R]



YMMV
posted by devbrain at 10:04 AM on July 8, 2009 [1 favorite]


This sounds like what RewriteBase was designed for. As long as the path component of the URL in question is the same for both servers, then you can just set that with RewriteBase and then use relative paths just as you theorized above. So, if the main site is at http://www.example.com/thesite/ and the test site is at http://test.example.com/thesite/, then you can just put "RewriteBase /thesite/" before the various relatively-defined rules. If they're both at the top level URL of their respective domains, then you'll want just "RewriteBase /".
posted by boaz at 10:34 AM on July 8, 2009


Response by poster: devbrain - if that works as it appears, that's exactly what I'm after. Thank you!

I tried looking through the documentation for the E flag, and it was as superbly vague as I was expecting. Without your follow up, I'm not sure I'd have ever worked that one out.
posted by twine42 at 11:40 AM on July 8, 2009


Response by poster: w00t!
posted by twine42 at 11:57 AM on July 8, 2009


You're welcome ..... but don't forget the YMMV. Moving your rewrite config into httpd.conf (or a file included from there) might be a better option for you.
posted by devbrain at 9:02 PM on July 8, 2009


« Older Hedging my bets.   |   Pretty Up My Word Newer »
This thread is closed to new comments.