Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

into this:

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 (,, 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 to return a file from the given customers data directory, i.e. /data/www/cms/app/customers/

share|improve this question
A little more detail - is this a one off to aid a content move, or is it going to be a part of daily use so to speak. Also, just one site or do you want something generic to apply everywhere? – Dan Oct 20 '11 at 14:16
@Dan A part of daily use. Just one site. – Henno Oct 20 '11 at 16:42
Will the destination be /data/www/cms/app/customers/ with foo**.com** or only "foo" without the ".com" (= is this a mistake or not)? This is important for the rewrite rule I may answer. – Olivier Pons Oct 24 '11 at 19:52
Real life example: /data/www/sisuhaldus/clients/riito_ee/galleries/4/pics/raua1.jpg, where riito_ee is a particular customer. – Henno Nov 2 '11 at 18:00
up vote 2 down vote accepted

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]


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

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/
RewriteRule ^/region/(.*)/$ /handle_regions.php \

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!

share|improve this answer
I updated the question and added more information. – Henno Oct 24 '11 at 18:27
Please update this answer to correspond to my comment under the question? – Henno Nov 3 '11 at 13:59
I've updated it! No offense, but 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! – Olivier Pons Nov 3 '11 at 15:25
That's OK. That's more than enough for me. Thank you. I left this previous comment only to be sure you knew that I had answered your comment under the question part, as I wasn't sure you get notifications when it's not your answer and you are not the first commenter (This would be really useful:…) – Henno Nov 4 '11 at 6:38
You welcome ;) ! – Olivier Pons Nov 4 '11 at 8:25

That will be something like:

RewriteEngine On
RewriteCond %{http_host} !^customer1\.com\/app\/$
RewriteRule ^/?(.*)$$1
share|improve this answer
HTTP_HOST is just the hostname - need to handle the /app match in the RewriteRule (and not unintentionally capture that part of the URL). – Shane Madden Oct 20 '11 at 14:43
Can I use %{http_host} in rewriting so that I wouldn't have to create a separate Rewrite rules for each and every customer? – Henno Oct 20 '11 at 16:35
I'm sorry but your RewriteRule is not correct (it will do a real redirect (301) because when you write http:// in a rewrite rule it does a real 301 whatever the rule is). – Olivier Pons Oct 21 '11 at 8:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.