httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Joshua Slive" <>
Subject Re: [users@httpd] Dynamic RewriteRule based on the URL
Date Thu, 07 Sep 2006 13:35:59 GMT
On 9/7/06, Norman Khine <> wrote:

> [1] <VirtualHost *:80>
> [2]    ServerName  domain.tld
> [3]    RewriteEngine on
> [4]    RewriteCond   %{*HTTP_HOST*}    *[^.]+*\.domain\.tld$
> [5]    RewriteRule   ^(.+)             %{HTTP_HOST}$1                        [C]
> [6]    RewriteRule   *([^.]+)*\.domain\.tld(.*) http://localhost:9080/*$1*$2 [P]
> [7]    RequestHeader set X-Base-Path %{*HTTP_HOST*}
> [8]    ErrorLog       /var/log/apache2/all-error_log
> [9]    CustomLog      /var/log/apache2/all-access_log common
> [0] </VirtualHost>
> I don't understand line [2], as the server gets the request http://folder.domain.tld
as this points to an A record IP address Apache will return the default page and it stops,
how do I inject the %{*HTTP_HOST*} value or is there some magic I am missing and don't see?
> Assuming that there is, Apache then switches the RewriteEngine on [3] and takes everything
before domain.tld - what is the meaning of [^.]+ ? then the [C] means continue to the next
RewriteRule which in my case needs to rewrite to an internal server and Proxy this i.e. [P],
but this does not work for me either ;'(
> Line [7] I need to set the X-Base-Path to the folder name.
> I would like to avoid using or any scripting but I am open to suggestions if
there is no other way ;)
> By the way, I don't have the www.'folder'.domain.tld in my URL - maybe this is the problem,
but don't see why?

I don't understand that last line.  But here is something that should be closer:
<VirtualHost *:80>
ServerName  domain.tld
ServerAlias *.domain.tld
RewriteEngine on
RewriteCond   %{HTTP_HOST}    ^([a-zA-Z_]+\.domain\.tld)$
RewriteRule (.+) http://localhost:9080/%1$1 [P,E=THEHOST:%1]

RequestHeader set X-Base-Path %{THEHOST}e
ErrorLog       /var/log/apache2/all-error_log
CustomLog      /var/log/apache2/all-access_log common

I don't know if this will work, but it is closer than what you have.
A few notes:
1. My regex matching the HOST is more strict to avoid clients playing
funny games with their host header and causing security problems.  It
accepts only hostnames containing letters and the underscore
2. In the RewriteRule %1 matches the regex from the RewriteCond and $1
matches the regex from the RewriteRule.
3. The funky stuff with THEHOST is necessary because the RequestHeader
directive cannot access mod_rewrite variables.  So I instead have
mod_rewrite shove HTTP_HOST in an environment variable.

One more note: I'm not actually sure whether RequestHeader can use env
variables at all.  You'd need to check the code to be sure.


The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:> for more info.
To unsubscribe, e-mail:
   "   from the digest:
For additional commands, e-mail:

View raw message