Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Cleaning URLs with Subfolders Introducing Constants

Robert Mews
Robert Mews
11,540 Points

BASE_URL and ROOT_PATH for non-localhost?

I'm using a live host for this exercise and am running into issues with defining the BASE_URL and ROOT_PATH. I'm able to get my pages to load. However, links are broken when using these constants. I can echo both my BASE_URL and ROOT_URL to the screen using some code I found on Stack Overflow. Here is the code for using to echo those:

$base_dir  = __DIR__; 
echo $base_dir;
echo "<br>";
$doc_root  = ($_SERVER["DOCUMENT_ROOT"]);
echo $doc_root;
echo "<br>";
$base_url  = preg_replace("!^{$doc_root}!", '', $base_dir); 
echo $base_url;

However, since my configuration is much different than a localhost, I'm not getting any links to work using the url path supplied in this video

"/shirts4mike_local/"

I'm really at a wits end here, and wondering how I would define a base URL and Root URL on a live server environment?

1 Answer

Jose Soto
Jose Soto
23,407 Points

Defining BASE_URL and ROOT_PATH on a live server environment should be no different that defining them on a local dev environment. You're simply telling PHP to define values to those strings.

Here is my server's document root value:

$SERVER["DOCUMENT_ROOT"] = /var/www/vhosts/website.com/httpdocs

This means that when I create a root-relative link, it will read from that directory.

For example, if I were to create a root-relative link of '/index.php'

<a href ="/index.php">Homepage</a>

the link would use the file located at /var/www/vhosts/website.com/httpdocs/index.php.

If I were to create a root-relative link of '/treehouse/index.php'

<a href ="/treehouse/index.php">Treehouse page</a>

the link would use the file located at /var/www/vhosts/website.com/httpdocs/treehouse/index.php

But now, let's say I want to copy all of my live server files into a sub-directory called 'dev' for development/testing. If I don't modify those links, they will read from the live site, not my 'dev' directory. So I will have to go through each link in my site and change it:

<!--Live Link -->
<a href ="/index.php">Homepage</a>

<!--Dev Link -->
<a href ="/dev/index.php">Homepage</a>


<!--Live Link -->
<a href ="/treehouse/index.php">Treehouse page</a>

<!--Dev Link -->
<a href ="/dev/treehouse/index.php">Treehouse page</a>

This change will need to be made on every relative path/link. Not to mention that once you are happy with your development changes, you will need to change them all back in order for the site to work in the live directory.

Here's where BASE_URL and ROOT_PATH can be used. You can follow the exact steps in the video just as though you were using localhost:

<?php
define("BASE_URL", "/dev/");
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/dev/");

BASE_URL can replace any root-relative links "/" in your code and ROOT_PATH can replace any include/require links that use the $SERVER["DOCUMENT_ROOT"] variable.

<!-- FROM THIS -->
<a href ="/index.php">Homepage</a>

<!-- TO THIS (be mindful of the forward slashes) -->
<a href ="<? echo BASE_URL; ?>index.php">Homepage</a>


<!-- FROM THIS -->
include($SERVER["DOCUMENT_ROOT"] . "/inc/header.php");

<!-- TO THIS (be mindful of the forward slashes) -->
include(ROOT_PATH . "inc/header.php");

One more thing. Since the BASE_URL and ROOT_PATH values need to be imported into your code, you cannot use the values to call the require method. That is why Randy used the relative path when requiring the config.php file:

require_once("../inc/config.php");

So now, you can move all of your site files to ANY directory and you only have to modify the config.php file once to have all of your links and includes work just fine.

I hope this helps a bit. If not, please provide some more details of the issue you're dealing with. Cheers!

Robert Mews
Robert Mews
11,540 Points

This was very helpful, Jose.

I've reconfigured my config.php file. Since I did not create a development enviornment, I'm working directly from my root directory. Here is my config.php code:

<?php

DEFINE("BASE_URL", "/");
DEFINE("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/");

Now when I add the BASE_URL and ROOT_PATH values to my links, I'm getting 404 Not Found errors with the following message: The requested URL /BASE_URLshirts.php was not found on this server.

Here is what my BASE_URL link directing to that page looks like within the header.php file:

<li class="shirts <?php if ($section == "shirts") { echo "on"; } ?>"><a href="<?php echo BASE_URL; ?>shirts.php">Shirts</a></li>

I must be doing something wrong with how I define the BASE_URL and ROOT_PATH, right?

EDIT: I should add that my root directory looks like this:

/home/sites/mydomain.com/public_html

Which seems odd given that most report that live server root paths look like this:

/var/www/vhosts/website.com/httpdocs

EDIT 2: I FIXED IT!! Yay. I suppose if you want to include the config.php file you probably don't want to use a relative path to call that file in other files vs. calling the config.php file with ROOT_PATH function.

Jose Soto
Jose Soto
23,407 Points

Great news that you fixed it! I'm curious about what your include looked like before and what it looks like now.