Web Development
Using PHP

Follow on
Twitter

Directories

Only the smallest websites will put all of the files required into one folder. With files in different folders, it is important to be able to link between the folders effectively.

The simplest links are between HTML files in the same directory. Here is an example of linking between two files in the same directory:

<html>
<body>
<a href="page2.html">Page 2</a> <br />
</body>
</html>
<html>
<body>
<a href="page1.html">Page 1</a> <br />
</body>
</html>

Try it out: [link]

This next example adds a new page, named page3.html in a new subdirectory, named subdir. Note that the links in page1.html go down into the subdir, and page3.html has a link that goes up one directory. This is done by using ../ in front of the file name. The two dots tell the comptuer to go "up" one directory.

<html>
<body>
<a href="page2.html">Page 2</a> <br />
<a href="subdir/page3.html">Page 3</a> subdir/page3.html<br />
</body>
</html>
<html>
<body>
<a href="../page1.html">Page 1</a> <br />
<a href="../page2.html">Page 2</a> <br />
</body>
</html>

Try it out: [link]

The next example shows how to extend the same concept to link into nested directories:

<html>
<body>
<a href="page2.html">Page 2</a> <br />
<a href="subdir/page3.html">Page 3</a> subdir/page3.html<br />
<a href="subdir/supersub/page4.html">Page 4</a> subdir/supersub/page4.html<br />
</body>
</html>
<html>
<body>
<a href="../page1.html">Page 1</a> <br />
<a href="../page2.html">Page 2</a> <br />
<a href="supersub/page4.html">Page 4</a> <br />
</body>
</html>
<html>
<body>
<a href="../../page1.html">Page 1</a> <br />
<a href="../../page2.html">Page 2</a> <br />
<a href="../page3.html">Page 3</a> <br />
</body>
</html>

Try it out: [link]

URLs may be absolute or relative. The URLs shown so far have all been relative URLs. Relative URLs give directions from where the user is at, to where the user would like to go. If possible, it is best to use relative URLs. The main advantage of relative URLs it is possible to move the files into a different directory, or even a different website and still have the links work. Absolute URLs will not take into account where the user is at currently, and always link to the same place. This is the right thing to do when linking to other websites, or other major sections of a website. It is a bad idea when creating links between pages of a website, because any moving of directories will cause every link in the website to need updating.

The next example shows two kinds of absolute URLs. The first gives an absolute path. Any path that begins with a / is an absolute path. It is possible to take an absolute path, and change the name of the website and still have the path work. The second also specifies the website name. It is not possible to update the website name and not have to also update the link.

<a href="page2.html">Page 2</a> <br />
<a href="subdir/page3.html">Page 3</a> subdir/page3.html<br />
<a href="/directory_test/subdir/page3.html">Page 3</a> 
/directory_test/subdir/page3.html<br />
<a href="http://localhost/directory_test/subdir/page3.html">Page 3</a> 
http://localhost/directory_test/subdir/page3.html<br />
<a href="subdir/supersub/page4.html">Page 4</a> subdir/supersub/page4.html<br />

Try it out: [link]

If a web developer wants to link to a particular directory on a webside, he or she could use the following code:

<a href="http://cs.simpson.edu/mydir">Link to mydir on simpson webpage</a>
The webserver for Simpson will first check to see if there is a page or a web application called mydir. There is not, so it will then see if there is a directory called mydir. There is, so the website will forward the user to the directory link:
http://cs.simpson.edu/mydir/
Note the trailing slash on the link. This request and forward requires and extra round trip to and from the server. The user's experience, and the load on the server, can be improved if the link to the directory was done with a trailing slash in the first place.

If a website links to a directory, the web server will look for an index file, such as index.html or index.php. If an index file does not exist, the web server will look to see if it can create a directory listing of all the files in the directory. It is usually a bad idea for security reasons to allow this to happen on a production web server. In the apach.conf there is a configuration setting to turn this directory listing feature off.

You are not logged in. Log in here and track your progress.