My dev server is http://localhost/mygreatapp
and my production server is http://www.myawesomesite.com
but maybe someday it will migrate to http://www.myawesomesite.com/mygreatapp
In these cases mygreatapp
is a virtual directory.
I would like a php function to return the correct website roots without having to hard-code anything (e.g., mygreatapp
), regardless if it's off the default website or a virtual directory. In either case, that's where all the relative paths to the files begins.
I have looked at the similar questions and not found a solution that does not involve hard-coding, which would then involve having to update the value for each environment, painful and error prone.
This will get my by for now, but I'll need to adjust it if I go into a subfolder on a linux machine.
public static function WebRoot(){
$virtualdir = '';
if(isset($_SERVER['APPL_MD_PATH'])){
$virtualdir = substr($_SERVER['APPL_MD_PATH'], strpos($_SERVER['APPL_MD_PATH'], '/ROOT/') + strlen('/ROOT/'));
}
return 'https://'.$_SERVER['SERVER_NAME'].'/'.$virtualdir.'/';
}