Forming URLs
URL stands for Uniform Resource Locator, and it is essentially
the "address" of a particular file on your web server.
URL Syntax
The URL is formed like this:
protocol:://server/directory/file
There are several protocols that you can use in an URL:
- http for HTML files
- https for secure HTML files
- ftp for file transfer
- file for files on your local PC
The server portion corresponds to either your domain name, or
the domain name of your ISP. This can also be written as an
IP address. The following are all valid server names for a URL:
- www.enscript.com
- enscript.com
- myhost.com
- 209.126.128.27
The directory/file portion of the URL depends on where the file
resides on your web server. This is similar to the UNIX
path for the file, but not exactly. Also, note that index.html
is assumed if you leave off the /file portion of the URL (actually,
there is a sequence of files that are looked for: index.html,
index.shtml, index.cgi, default.html ...).
UNIX path vs. URL
When the ISP sets up your account, they must define a "root"
HTML directory for your site. That is not the same as the
UNIX root directory. The root directory for your site is the
top level directory for your HTML files. Some examples:
/usr/home/ashley/public_html
/disk33/usr/kidstuff/www
/u34/www12312/html
The following diagrams depict the some of the common ways ISP
set up web site directories.
Fig 1. Site Directory
Setup A |
Fig 2. Site Directory
Setup B |
In Setup A, the UNIX path for the root HTML directory is
/home/ashley/www. In Setup B, it is /home/ashley/public_html.
The corresponding URL, however, does not include the full
UNIX path, but rather an abbreviation starting from the root
HTML directory for the web site.
If you have your own domain, your root URL is something
like this:
http://www.kidstuff.com
However if you are using space on a server without your
own domain, it probably looks something like this:
http://www.mindspring.com/~ashley
Any subdirectory of this root HTML directory can be accessed
by appending the subdirectory name to the URL, like
http://www.kidstuff.com/images
or
http://www.mindspring.com/~ashley/images
The distinction between UNIX path and the URL is very important
in installing CGI script. Very often you will be asked to
supply both. For example, from the configuration file for
Links 2.0 from Gossamer
Threads:
# PATH and URL of Admin CGI directory. No Trailing Slash.
$db_script_path = "/home/ashley/www/links/cgi-bin/admin";
$db_dir_url = "http://www.enscript.com/links/cgi-bin/admin";
URL Shorthand
From within an HTML file, you can use a shorthand notation
to reference other files on the same server. The root HTML
directory is indicated by starting a URL with a "/", such
as
<A HREF=/index.html>
This means you can now have a shortcut to your root HTML directory
from any where on the same server. (If you don't have your own
domain, the shortcut would be something like "/~ashley/")
You may of course also use the full URL in your HTML files:
<A HREF=http://www.kidstuff.com>
or
<A HREF=http://www.mindspring.com/~ashley/images>
All the shortcut lets you do is omit the protocol and server
name (http://www.kidstuff.com or http://www.mindspring.com)
Absolute vs. Relative Links
An absolute link starts with "/" (the root HTML directory)
and spells out the entire path to the file on your site. A
relative link is more like shorthand -- if the two files are
in the same directory, the link doesn't have to spell out
the entire path.
For example, if you have file called test.html in the same
directory as index.html, you can put
<A HREF=test.html>
inside the index.html code. You can also specify subdirectories,
like
<IMG SRC=images/test.gif>
More examples:
Fig 3. Illustration
of absolute vs. relative links |
All of these are valid ABSOLUTE HREFs inside fileA.html:
<A HREF="/fileB.html">
<IMG SRC="/mystuff/imageA.jpg">
<IMG SRC="/mystuff/someimages/imgB.jpg">
These are all valid RELATIVE HREFs inside fileA.html:
<A HREF="fileB.html">
<IMG SRC="mystuff/imageA.jpg">
<IMG SRC="mystuff/someimages/imgB.jpg">
And more complicated cases....
All of these are valid ABSOLUTE HREFs inside fileC.html:
<A HREF="/fileB.html">
<IMG SRC="/mystuff/imageA.jpg">
<IMG SRC="/mystuff/someimages/imageB.jpg">
These are all valid RELATIVE HREFs inside fileC.html:
<A HREF="../fileB.html">
<IMG SRC="imageA.jpg">
<IMG SRC="someimages/imageB.jpg">
See how the absolute tags are valid no matter which file
you're in? The relative tags are dependent on the location
of the HTML file. |
Linking to CGI scripts
For cgi scripts, you may have to put them in a special directory
in order to tell the server to "execute" them instead of just
displaying them. Usually, this is in a directory called cgi-bin
which has a special meaning to the server.
A URL of
http://www.kidstuff.com/cgi-bin/search.pl
will execute the search.pl CGI script from the cgi-bin directory.
Note that the cgi-bin directory may not be subdirctory of
the html directory, even though the URL makes it look like
it is (see the differences between Figures 1 and 2 above).
The actual path for the cgi-bin URL is determined by the ScriptAlias
directive in the server configuration.
This is important because from within a CGI script you should
generally not link to another file (especially non-CGI files
like site images) using a relative link. Using absolute link
notation is more straightforward since the CGI script may
be in a directory outside of your web root. If you do use
relative URL notation, make sure it is relative from the perspective
of the CGI script, and not from the perspective of your main
html directory.
Related Links
|