function url(){ if(isset($_SERVER['HTTPS'])){ $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http"; } else{ $protocol = 'http'; } return $protocol . "://" . $_SERVER['HTTP_HOST']; }
For example with the function above, it works fine if I work with the same directory, but if I make a sub directory, and work in it, it will give me the location of the sub directory also for example. I just want example.com
but it gives me example.com/sub
if I'm working in the folder sub
. If I'm using the main directory,the function works fine. Is there an alternative to $_SERVER['HTTP_HOST']
?
Or how could I fix my function/code to get the main url only? Thanks.
<? php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
The base domain is the umbrella under which an entire site resides. A base domain consists of only two of the URL elements: Domain Name. Top Level Domain (TLD)
Use SERVER_NAME
.
echo $_SERVER['SERVER_NAME']; //Outputs www.example.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With