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 trialAdel Akbar
4,977 PointsRoot Relative Links don't work in my localhost
I followed the video and I did everything I was supposed to do. I added the (/) slash before all my links but the links stopped working. I have no idea whats going on. Or is my xampp outdated?
4 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Adel,
As Simon mentioned, your "commerceWeb" folder is not treated as the site root. The drawback to changing your links like that is that you will have to change them all when you go live assuming this was a real project.
One thing that you can look at to solve this problem is setting up a virtual host. This will allow you to use whatever folder you want to put your project in and that folder will be treated as a root folder.
Then you can go back to using /css/style.css
and it will work both locally and when you upload to a host.
Here's a link on how to set it up: http://sawmac.com/xampp/virtualhosts/
Justin Perry
12,530 PointsOne way to deal with this is to store the root relative file path with the additional directory as a variable. When the site is to be put onto a remote server, simply change the value of the variable to an empty string.
Leigh Maher
21,830 PointsHi Justin, any clues as to how do this this?
James Ma
19,063 Pointsjust did it right now...
in your inc/header.php filee
<?php $relative = /name_of_folder/ (in my case it was shirts4mike_start) ?>
then in your links put<link href="<?php echo $relative ; ?> css/style.css" />(or whatever you link you want..
Justin Perry
12,530 PointsLeigh, say for example you have your project in a subdirectory inside of xampp/ wamp, and the subdirectory is called shirt_project. When you launch to a remote server, you will not be using this directory anymore, but you need it on your localhost. At the top of your file (or in an include) create a variable, for example:
<?php
$relative = "/shirt_project/"
?>
Inside of your links, open up a PHP block, and echo out the variable, like so:
<a href = "<?php echo $relative; ?> shirts.php"><p>Shirt stuff here</p></a>
Then when you go live to a remote server, set the it to an empty string. You might need to adjust the slashes to your particular folder setup. Even though constants are supposed to remain unchanged, you could also set the value to a constant, and then just change the constant itself when going live. I hope this helps!
Leigh Maher
21,830 PointsThanks for this, Justin. I went onto the next video and it actually goes through this process, pretty much as you've described here. He sets it up as a constant in a separate config.php file, and I've just this minute got it working. Thanks so much for replying.
Simon Woodard
6,545 PointsWhen dealing with xampp, the root folder is the xampp 'htdocs' folder as you found out... remember you may have to adjust the root relative address if you place the site online with a host
Justin Perry
12,530 PointsLeigh, no problem, glad it worked out for you.
Adel Akbar
4,977 PointsAdel Akbar
4,977 PointsI figured it out. My whole website was in a folder named commerceWeb. So in my root relative links i had to change the links to /commerceWeb/css/style.css . Hope that helps everyone else who faces the same probems.