HTML, XHTML, and CSS, Sixth Edition

Uniform Resource Locator, or URL, is a fancy name for address. 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 scheme. It tells the browser how to deal with the file that it is about to open. The most common scheme you will see is HTTP, or Hypertext Transfer Protocol. It is used to access Web pages (Figure 1.16).

Figure 1.16. Your basic URL contains a scheme, server name, path, 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 (Figure 1.17). In this case the URL refers to the default file in the last directory in the path (which generally corresponds to the home page), often called index.html or default.htm.

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 names are index.html and default.htm.

Other common schemes are HTTPS, for secure Web pages; FTP (File Transfer Protocol) for downloading files (Figure 1.18); Mailto, for sending email (Figure 1.19) ; and File, for accessing files on a local hard disk or local file sharing networks (Figure 1.20).

Figure 1.18. When the user clicks this URL, the browser will begin an FTP transfer of the file prog.exe.

Figure 1.19. A URL for an email address includes the mailto scheme followed by a colon but no forward slashes, and then the email address itself.

Figure 1.20. To reference a file on a local Windows machine, use the file scheme. For Macintosh, use file:///Harddisk/path/filename. 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 letters.

Absolute URLs

URLs can be either absolute or relative. An absolute URL 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 bearing 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 URLsyouarehere.html 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

File name

Absolute URL (can be used anywhere)

Relative URL (only works in youarehere.html)

index.html

www.site.com/web/index.html

index.html

image.gif

www.site.com/web/images/image.gif

images/image.gif

data.html

www.site.com/info/data.html

../info/data.html

homepg.html

www.remote.com/pub/homepg.html

(none: use absolute)

info.html

www.remote.com/bcn/info.html

(none: use absolute)

Relative URLs

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 relative 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 relative URL 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 (Figure 1.ch01fig22). 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.ch01fig23).

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 higher level of the file hierarchy, use two periods and a forward slash (Figure 1.ch01fig24). 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.

Категории