Search code examples
phpmacosurlpathdocument-root

Changing the DOCUMENT_ROOT or a better approach


Is there a better way to take set the path for my local environment. Here is what i am trying to do

<?php
if ($_SERVER['SERVER_NAME'] == 'localhost') {
    define("URL", "/mysite");
}else{
    define("URL", "");
}
?>

the only problem with this approach is I need to litter my code with stuff like this

<link rel="stylesheet" href="<?php echo URL; ?>/css/main-style.css" type="text/css" media="all" />
<link rel="stylesheet" href="<?php echo URL; ?>/css/home.css" type="text/css" />
<script src="<?php echo URL; ?>/js/jquery.js?v=1.5.2" type="text/javascript"></script>
<?php include($_SERVER['DOCUMENT_ROOT'] . URL . '/inc/header-inc.php'); ?>

And i have to find every absolute path in my site and add the URL in front which is cluttering the code with this. I am on MAc osx and my DOCUMENT_ROOT is /Users/alex/Sites

I can change it to /Users/alex/Sites/mysite but what happens when I need mysite2


Solution

  • Try this approach