I am trying to achieve a means by which I can get the base URL of a website without having to define it manually and regardless of any other additional query strings or folders added later
Example
If the url is
The output should be
respectively
I have tried using this and other related variations
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
But it always changes based on the added values in the url.
Can I please get some assistance.
Thanks
For absolute paths, it is sometimes helpful to define constants like:
// which page are we on? eg /some/path/to/script.php
if( ! defined( 'WEB_PATH' ) ) define('WEB_PATH', $_SERVER['PHP_SELF']);
// web root dir. eg /some/path/to
//if( ! defined( 'WEB_ROOT' ) ) define('WEB_ROOT', dirname(WEB_PATH));
if( ! defined( 'WEB_ROOT' ) ) {
$path = dirname(WEB_PATH);
// avoid "//" at root
if ( $path == '/' ) $path = '';
define('WEB_ROOT', $path);
}
// what is the file name only? // eg script.php
if( ! defined( 'WEB_FILE' ) ) define('WEB_FILE', basename(WEB_PATH));
// physical file path (system). eg: /var/www/html/yoursite
if( ! defined( 'ABS_ROOT' ) ) define('ABS_ROOT', dirname(__FILE__));