Question

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!

Was it helpful?

Solution

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'], '.'));

OTHER TIPS

You need to use

RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*)$ %1/$1 [L]
  • %1 is a subdomain name
  • $1 is a request
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top