Uniform Resource Locator
, is a fancy
. It contains information about where a file is and what a browser should do with it. Each file on the Internet has a unique URL.
The first part of the URL is called the
the browser how to deal with the file that it is about to
. The most common scheme you will see is HTTP, or
Hypertext Transfer Protocol
. It is used to access Web pages
Figure 1.16. Your basic URL contains a scheme, server name,
, and file name.
The second part of the URL is the name of the server where the file is located, followed by the path that leads to the file and the file's name itself. Sometimes, a URL ends in a trailing forward slash with no file name given
. In this case the URL refers to the default file in the last directory in the path (which
corresponds to the home page), often called
Figure 1.17. A URL with a trailing forward slash and no file name points to the default file in the last directory named (in this case the liz directory). Some common default file
Other common schemes are HTTPS, for secure Web pages; FTP (File Transfer Protocol) for downloading files
; Mailto, for sending email
; and File, for accessing files on a local hard disk or local file sharing networks
Figure 1.18. When the
clicks this URL, the browser will begin an FTP transfer of the file
Figure 1.19. A URL for an email address includes the
scheme followed by a
but no forward
, and then the email address itself.
Figure 1.20. To reference a file on a local Windows machine, use the
scheme. For Macintosh, use
. No vertical bar is required. (This sometimes works for Windows as well.)
A scheme is generally followed by a colon and two forward slashes. Mailto and News are exceptions; these take only a colon.
Notice that the File scheme uses three slashes. That's because the host, which in other schemes goes between the second and third slashes, is assumed to be the local computer. Always type schemes in lowercase
URLs can be either absolute or relative. An
shows the entire path to the file, including the scheme, server name, the complete path, and the file name itself. An absolute URL is analogous to a complete street address, including name, street and number, city, state, zip code, and country. No matter where a letter is sent from, the post office will be able to find the recipient. In terms of URLs, this means that the location of the absolute URL itself has no
on the location of the actual file referencedwhether it is in a Web page on your server or on mine, an absolute URL will look exactly the same.
When you're referencing a file from someone else's server, you'll always use an absolute URL. You'll also need to use absolute URLs for FTP sites, or any kind of URL that doesn't use an HTTP protocol.
Figure 1.21. The document that contains the URLs
in this caseis the reference point for relative URLs. In other words, relative URLs are relative to that file's location on the server. Absolute URLs don't care where they are located.
Absolute URLs vs. Relative URLs
Absolute URL (can be used
Relative URL (only works in youarehere.html)
(none: use absolute)
(none: use absolute)
To give you directions to my neighbor's house, instead of giving her complete address, I might just say "it's three doors down on the right". This is a
addresswhere it points to depends on where the information originates. With the same information in a different city, you'd never find my neighbor.
In the same way, a
describes the location of the desired file with reference to the location of the file that contains the URL itself. So, you might have the URL say something like "show the xyz image that's in the same directory as this page".
The relative URL for a file that is in the same directory as the current page (that is, the one containing the URL in question) is simply the file name and extension
. You create the URL for a file in a subdirectory of the current directory by typing the name of the subdirectory followed by a forward slash and then the name and extension of the desired file
Figure 1.22. The relative URL for a file in the same folder (see Figure 1.21) as the file that contains the link is just the file's name and extension.
Figure 1.23. For a file that is within a folder inside the current folder (see Figure 1.21), add the folder's name and a forward slash in front of the file name.
To reference a file in a directory at a
level of the file hierarchy, use two periods and a forward slash
. You can combine and repeat the two periods and forward slash to reference any file on the same hard disk as the current file.
Figure 1.24. This file, as you can see in Figure 1.21, is in a folder that is inside the folder that contains the current folder (whew!). In that case, you use two periods and a slash to go up a level, and then note the subdirectory, followed by a forward slash, followed by the file name.
Generally, for files on the same server, you should always use relative URLs. They're much easier to type and they make it easy to move your pages from a local system to a server. As long as the relative position of each file remains constant, the links will work correctly.
One added advantage of relative URLs is that you don't have to type the schemeas long as it's HTTP.