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 Using Absolute Server Paths

Jonathan Brits
Jonathan Brits
865 Points

Absolute vs Relative path

What is the best practice for web? Is there any reason not to use absolute or relative?

I would say relative paths are preferred if possible. There are a couple reasons for this, but if you are building a more large scale application, using relative paths allows you to move it potentially to another domain name and not have to replace all the "http://www._____/" parts, because it will be able to link properly regardless of the domain with relative links. That is probably the largest benefit, but it is considered a 'good practice' to use relative links as much as possible in a project. (linking to another website obviously requires absolute paths.) To read more on the benefits, this article goes into a ton of depth about it:

Also: just saw you might be referring to PHP, for server-side stuff definitely use relative paths whenever you can. It will be significantly faster because the server will probably have to make a full HTTP request to get a file specified at an absolute path. That would add a ton of time to the request in comparison to a relative path.

3 Answers

Andrew Shook
Andrew Shook
31,709 Points

Jonathan, I always tend to use a root relative path in my projects. I usually keep a config file in my projects root directory, and in that file I usually have a piece of code that looks like this:

define( "ROOT_PATH", dirname( __FILE__ ) . DIRECTORY_SEPARATOR );

Since I keep the config file in the root directory, this constant will store the file path to the root directory plus the either a "/" on unix type systems or "\" on Windows systems. Then when I need to include a file I do something like this:

include(ROOT_PATH . "inc/some_script.php");

From what I've seen this is a very common way of handling path's using php.

Kevin Korte
Kevin Korte
28,148 Points

I'm just going to leave this here:

I think when using a server side language, absolute URL's are no harder to manage than relative URL's.

When you're site is a static HTML site, I would use relative URL's whenever possible just for my own sanity.

https://yoast.com/relative-urls-issues/

So your saying accessing http://website.com/user.json is just as fast as accessing /user.json serverside via a serverside language like PHP, ex:

<?php
define( "ROOT_DIR", dirname( __FILE__ ) . DIRECTORY_SEPARATOR );
$v = file_get_contents(ROOT_DIR.'/user.json');
// Is as fast as and as easy to maintain as:
$v = file_get_contents('http://website.com/user.json');

I've always thought otherwise, but maybe I'm incorrect?

Kevin Korte
Kevin Korte
28,148 Points

I'm not sure it's as fast. But...my opinion is that it hardly matters. If you built an application that ran slower than you wanted to, there are a number of things to test for that I would put as higher importance over URL structure for speed. If I could benchmark speeds with absolute URLs, and saw that relative URLs came in consistently faster, than that would be a no brainer.

My opinion could be swayed with new information, but I've never seen a blog post yet where someone benchmarked the site speed using both relative and absolute file paths, and could show with actual data that there was a consistent speed increase with relative paths. I've only ever seen it said, but never proven that I've seen. Maybe this blog post exists, I just haven seen it.

I tend to question things like this where I'm just "told" that relative URLs are faster. My stance here is not to be argumentative, but rather to question what we have been "told", requiring some extra thought. Even the article I posted from Yoast should be questioned with his stance the other way. Ultimately the actual numbers will tell the story.

Hi Jonathan,

When using PHP, and you have nested directories on your website it is going to become necessary to use a method like Andrew's. $_SERVER["DOCUMENT_ROOT"] is another one. I liked the article that Kevin linked to. Below is the important part of the article, on my opinion.

*Absolute URLs everywhere

Whenever WordPress outputs a URL, it's always a full, absolute URL. For the domain name part of that it uses the domain you set in the General settings. This is the type of solution everyone should use: the domain name should be in a configuration file, this would allow you to still easily migrate between development environment and live environment by just using different configuration files.*

For example, when you have files in nested directories calling a css file from a header file that was included with include or require statement the browser will look at the relative path (if used), for the css file and request tHe path from where the original file was located, even though the header file is located in a different directory. If you use a method like Andrew's the server will output the correct URL to the browser. I hope I didn't confuse you to much. As you use PHP more you will begin to understand the issue better.

Jeff