Score:0

Writing a RewriteRule based on X-Forwarded-Host

tg flag

I have an Apache webserver behind a load balancer which proxies request to Apache and have problem regarding configuring directories.

Here is my scenario:

  1. Upper Load Balancer proxies request to Apache with adding original Hostname in X-Forwarded-Host.
  2. I should decide which directory to serve based on X-Forwarded-Host header value.
  3. Incoming requests have X-Forwarded-Host headers like:
    test1.example.com
    test2.example.com
    test3.example.com
  4. Here is my desired behavior:
    Each request must be served from:
    %{DocumentRoot}/test1/
    %{DocumentRoot}/test2/
    %{DocumentRoot}/test3/
  5. I wrote something like this, but it needs to be corrected:
RewriteCond "%{HTTP:X-Forwarded-Host}" "^[^.]*"
RewriteRule "^(.*)" "%{DOCUMENT_ROOT}/%1/$1" [L]

Any help on how to write Rewrite Rule?

Score:0
by flag

You need to put your RewriteCond pattern in parenthesis, like this:

RewriteCond "%{HTTP:X-Forwarded-Host}" "^([^.]*)"
RewriteRule "^(.*)" "%{DOCUMENT_ROOT}/%1/$1" [L]
Ageniz Shop avatar
tg flag
Thanks it worked!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.