views:

311

answers:

2

I have the basic code to rewrite a subdomain to another page. But how do I use this to get the path of the directory specified within it and pass it to my script as well as the subdomain itself?

Current code

RewriteCond %{HTTP_HOST} ^([^.]+)(\.example\.com)$
RewriteRule ^$ handle.php [L]

I'd like to provide handle.php with the file path specified after *.example.com and also the subdomain name in the form handle.php?path=x&subdomain=y.

Any help much appreciated!

A: 

You can access that information with $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] (does also contain the query):

$path = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
$subdomain = substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], '.'));
Gumbo
A: 

You need use RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*)$ %1/$1 [L]

%1 is a subdomain name

$1 is a request