views:

1032

answers:

2

I would like mod_rewrite in an .htaccess file to link to a mysql database to provide me with mapping information.

Specifically, I am using a single code base to host multiple sites so..if a user requests an image, for example: http://www.example.com/images/car.jpg

this is going to hit my server and many other sites will also be hitting that /images folder, so I need Apache to reply with: /home/example/pubic_html/images/7383/car.jpg

Notice the injection of "7383" which is an example site id for that user.

Basically, I want to map between example.com and 7383 using the mysql database and then get the correct file to the user.

Any ideas?

+3  A: 

You could use a script as a rewrite map to get the real path.

Gumbo
Been there. Done that. Works perfectly.
David Schmitt
+1  A: 

RewriteMap is the correct Apache functionality to use. Thank you Gumbo.

Somethings to note as I worked through this:

  • RewriteMap goes in your Apache config instead of in the .htaccess file
  • I wrote a script to build a mapping text file that will perform the mapping that I had above. It basically dumps from the database whatever is necessary -- since mine does not change every minute, I can just run a periodic dump into this text file whenever mapping is added or changed.

Works like a champ. Thanks Apache for this wonderful tool.

Simon