Im trying to redirect requests for a wildcard domain to a sub-directory.
ie. -->

I dont know how to get the subdomain name to use in the rewrite rule.

Final Solution:

RewriteCond %{HTTP_HOST} !^blah\.domain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /%1/$1 [L]

Or as pointed out by pilif

RewriteCond %{HTTP_HOST} ^([^.]+)\.media\.xnet\.tk$
+7  A: 

You should have a look at the URL Rewriting Guide from the apache documentation.

The following is untested, but it should to the trick:

RewriteCond %{HTTP_HOST} ^([^.]+)\.blah\.domain\.com$
RewriteRule ^/(.*)$ $1 [L,R]

This only works if the subdomain contain no dots. Otherwise, you'd have to alter the Regexp in RewriteCond to match any character which should still work due to the anchoring, but this certainly feels saver.

+2  A: 

Try this:

RewriteCond %{HTTP_HOST} (.+)\.blah\.domain\.com
RewriteRule ^(.+)$ /%1/$1 [L]

@pilif (see comment): Okay, that's true. I just copied a .htaccess that I use on one of my projects. Guess it has a slightly different approach :)

your solution does not redirect to but just rewrites to, which is not how I read the original question.
I guess that is OK if they are both served by the same VirtualHost. In particular, doing it this way means the client doesn't see the redirection - they still see
Ted Percival

Thanks, but i cant get it to work. here is what i have so far:

<VirtualHost *:80>
    ServerAlias *
    DocumentRoot /var/www/media

    RewriteEngine On
    RewriteLog "/var/log/apache2/rewrite.log"
    RewriteLogLevel 4
    RewriteCond ${HTTP_HOST} ^([^\.]+)\.media\.xnet\.tk$
    RewriteRule ^(.*)$ /%1/$1 [L]

From Log: RewriteCond: input='${HTTP_HOST}' pattern='^([^\.]+)\.media\.xnet\.tk$' => not-matched

Make sure your request is using HTTP/1.1 if you want to match the Host header like that.
Ted Percival
+1  A: 


your RewriteCond line is wrong. The expansion of the variable is triggered with %, not $.

RewriteCond %{HTTP_HOST} ^([^\.]+)\.media\.xnet\.tk$

that should do the trick

HA! the one character typo strikes again :(