Subdomain as part of REQUEST_URI

1.3k Views Asked by At

I have a local site: http://domain.loc/ and that site have subdomain: http://sub.domain.loc/. How can I configure my Apache server || PHP to make him return global variable: $_SERVER['REQUEST_URI'] with my subdomain name ("sub" in this case)?

Examples:

If I'll open the page http://domain.loc/ then $_SERVER['REQUEST_URI'] will be equal to /
If I'll open the page http://sub.domain.loc/ then $_SERVER['REQUEST_URI'] will be equal to /sub/

2

There are 2 best solutions below

3
On BEST ANSWER

Not possible. REQUEST_URI is what comes AFTER the hostname component of a url:

http://example.com/foo/bar/baz
           ^--- HTTP_HOST
                    ^---- REQUEST_URI

You can certainly have your subdomain name in the URI component, but that's something YOU have to do, and something Apache+PHP won't. They're not going to rewrite fundamental definitions of URLs just to suit you.

0
On

$_SERVER['HTTP_HOST'] returns the domain with the current subdomain

$parsedUrl = parse_url($_SERVER['HTTP_HOST']);

$host = explode('.', $parsedUrl['host']);

$subdomain = $host[0];
echo $subdomain;