URLs
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.
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 generally corresponds to the home page), often
called index.html or default.htm.
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.
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.
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. 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.
To reference a file in a directory at a higher 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.
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.
|
0 comments:
Post a Comment