Rewriting URL based on hostname

apache-2.2mod-rewriterewrite

Is it possible to use .htaccess file to rewrite this:

http://customer1.com/app/assets/css

into this:

http://customer1.com/app/customers/customer1/assets/css?

If it is then how?

EDIT: The goal is to avoid any updates to .htaccess when a new customer is added
EDIT2:

I see that my question has been too vague. I have a CMS that is being used for lots of different customers (www.foo.com, www.bar.com, www.somerandomname.com). Each customer has it's unique files (design, uploads) in a directory /data/www/cms/customers/XXXXXX/ where XXXXXX is the name of the customer. Apache config file has default DocumentRoot set on /data/www/cms/.

What I'm trying to do is to have the URL http://www.foo.com/app/assets/css/bg.png to return a file from the given customers data directory, i.e. /data/www/cms/app/customers/foo.com/assets/css/bg.png.

Best Answer

I don't know about .htaccess file but I'm pretty sure this would work:

RewriteEngine On
RewriteCond %{HTTP_HOST} customer([0-9]+)\.com
RewriteRule ^/app/(.*) /app/customers/customer%1/$1 [QSA]

Explanation:

RewriteCond %{HTTP_HOST} customer([0-9]+)\.com

will put in the variable '%1' the customer number i.e. '1'

RewriteRule ^/app/(.*) /app/customers/customer%1/$1 [QSA]

Means: if the path begins with '/app/' then rewrite it to /app/customers/customer and add the variable '%1' (i.e. '1' in our example) so it should work with what you're asking for: rewrite to http://customer1.com/app/customers/customer1/.

I'm sorry but your question is not really precise, and you should either giver more examples, or be more specific to what you want to do, but I did my best to help you.

You may use the RewriteMap directive as well. Imagine you put all your clients websites and the right directories in it like this:

alsace                     1   
aquitaine                  2   
auvergne                   3   
basse-normandie            4   
bourgogne                  5   
bretagne                   6   
centre                     7   
champagne-ardenne          8   
corse                      9   
franche-comte              10  
haute-normandie            11  

then I use a RewriteRule like this:

RewriteMap mapregions dbm:/web/htdocs/maps/regions.map
RewriteRule ^/region/(.*)/$ /handle_regions.php \
  [NC,E=REGION:${mapregions:$1|notfound}]

So what it does is basically: if you ask for an url like /region/(.*)/ it redirects to /handle_regions.php and sets an environment variable REGION Then I just check if it was found: if not, I stop:

RewriteCond %{ENV:REGION} notfound
# "not found" = 404 :
RewriteRule .* - [R=404,L]

And then here you are. I'm sorry but with all the things I've just said, you have enough material to solve your problem. I don't have the time to do all the stuff for you, sorry man, I did the best I could to help you... and I hope this helps!