/ - why that slash is good
January 24th, 2009
This might be a bit basic for a lot of you, but it’s something a lot of people don’t really know about, and it kind of surprises me.
Do you know the difference between:
images/someimage.jpg
../images/someimage.jpg
/images/someimage.jpg
The first two are relative to the document, the last one is relative to the root directory. This is useful when you’re coding large sites with sub sections. Normally, for a five or six page site I’ll keep all my pages in my main directory, and then use an includes folder, a css folder, and an images folder. On larger sites, I’ll use more directories. So I may have ten or more folders, each containing up to ten or more files in it. If I need to get into that images folder, I have two options (3 if you use the full url). I can use ../images/someimage.jpg which means up one directory, then into the images folder or, I can use /images/someimage.jpg. Using the second method, it doesn’t matter what directory the page is in, it goes to the images folder from the site’s home directory.
Why is this important? Most times you don’t really move pages around, but in order to cut down on the amount of repeat coding, you do use includes. So, if I’m using an include for my navigation, the home page which is in the root directory is going to be at a different depth then a page in a subfolder. If I use site relevant paths for all my pages in the navigation that I’m including, then it won’t matter what depth the file is, or which directory it is placed in.
/about/index.php will take you into the about folder whether you’re at the home page, index.php, or if you’re in the about directory already at about/contact.php or in a different directory like work/print.php.
Tags: directory structure, paths
This entry was posted on Saturday, January 24th, 2009 at 5:06 pm and is filed under html. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply