This is odd, but for some reason the $_SERVER[\"SCRIPT_URI\"]
will not return the domain name when I am in child/sub-pages but will only work on the main page.
This might be due to URL rewriting, you can try $_SERVER['REQUEST_URI'] instead if you want the path that was called in the url.
Also, SCRIPT_URI does not exist, the right syntax is REQUEST_URI:
echo $_SERVER["REQUEST_URI"];
It returns the whole path without the host and domain.
When in doubt
var_dump($_SERVER);
I was using $_SERVER[SCRIPT_URI] on my website http://www.a2zidx.com and a number of subdirectories like http://howto.a2zidx.com
I have had no problem with this for years and today got an error on a number of sites where $_SERVER[SCRIPT_URI] was not assigned. After contacting my isp they claim they have not made any changes but $_SERVER[SCRIPT_URI] no longer works. getenv('SCRIPT_URI') does not fail but returns a null string. Why this should happen suddenly after so many years I do not know. I ended up calling a function to go through various options to extract the filename which is what I wanted. Hope this covers everything. I had trouble including the function but checked "SCRIPT_NAME" "PHP_SELF" "SCRIPT_FILENAME "SCRIPT_URI"
Hope his helps.
If anyone knows why SCRIPT_URI would suddenly stop working I would love to know. The server is currently running Apache 2.4.
SCRIPT_URI
is not fond because it has no any value when your site run with root domain. it only come when you working with sub-directory.
HTTP_HOST
is not good idea when we working with sub-directory
use following code to get SCRIPT_URI
:
$SCRIPT_URI = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($_SERVER['SCRIPT_URL']),'/');
Depending on what you want, I'd use one of the following:
From the php docs
EDIT: Maybe PHP_SELF isn't the best. See comments.