0

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/

3
  • Can I ask you why do you want to do this. .htaccess is not ok for you?
    – vaso123
    Commented Dec 15, 2014 at 14:16
  • What's the use-case, and why not take $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] instead of only the uri? See PHP: $_SERVER - Manual.
    – Daniel W.
    Commented Dec 15, 2014 at 14:23
  • Recently I got a new project with subdomain. Controller, which have to load the main page of the subdomain, included by REQUEST_URI only!!! I don't know how... but it works on DEV and PRODUCTION servers. Can't make it works on my local machine. Commented Dec 15, 2014 at 14:41

2 Answers 2

2

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.

3
  • Looks like it possible... but only with nginx: stackoverflow.com/questions/18869930/… But I didn't try it. I don't have nginx on my local machine. Commented Dec 15, 2014 at 14:47
  • It's not a matter of nginx/apache, it's a matter of php_mod vs php-fpm. The project should be improved instead of hacking your own server to fit a bad implemented project.
    – Daniel W.
    Commented Dec 15, 2014 at 15:24
  • Yes, I think you right. But I'm can't do anything with it, because a huge part of the project which have to be rewritten for that is subrepo and using on other projects. But still thank you for explanation. Commented Dec 16, 2014 at 6:53
0

$_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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.