Absolute URLs use absolute pathnames.
But they start at the document root, not the filesystem root.
Relative URLs use relative pathnames.
They start at the directory that contains the current document.
The browser converts the relative URL into an absolute URL before sending a request to the server.
There are two main Web servers:
Apache
and
Microsoft Internet Information Server (IIS)
(and lots of ones that are more niche).
For concreteness, we'll look at the Apache Web Server.
mod_mime
;mod_ssl
.httpd.conf
httpd.conf
Example
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
ServerName
directive specifies the hostname.Listen
directive specifies the port that the server is listening on.DocumentRoot
is the directory that hosts all the web resources.DocumentRoot
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
DocumentRoot
pathname.http://www.example.org/fish/guppies.html
, the server will serve /var/www/html/fish/guppies.html
Assume the server's DocumentRoot
is /var/www/html
.
A browser sends a GET request for the following URL:
http://www.example.org/cats/persian.html
The server sends back /var/www/html/cats/persian.html
Similarly, if a browser sends a GET request for the following URL:
http://www.example.org/cats/manx.html
The server's response is a 404.
Similarly, if a browser sends a GET request for the following URL:
http://www.example.org/dogs/poodle.html
The server's response is a 404.
The browser has just fetched the following web page: http://www.example.org/cats/persian.html
The web page contains a hyperlink:
<a href="siamese.html>Siamese!</a>
The user clicks on this hyperlink.
So the browser sends a new request to http://www.example.org/cats/siamese.html
Again, the browser has just fetched the following:
http://www.example.org/cats/persian.html
The web page contains a hyperlink:
<a href="../fish/guppies.html>Guppies</a>
The user clicks on this hyperlink.
So the browser sends a new request to http://www.example.org/fish/guppies.html
Advice about how to write hyperlinks in web pages.
Absolute URL | Relative URL | |
---|---|---|
External link (i.e. on a different server) | scheme + hostname/IP address + absolute pathname | N/A |
Internal link (i.e. on the same server) | scheme + hostname/IP address + absolute pathname | relative pathname |
absolute pathname |
You want a link in siamese.html
to the RTE news.
An external link, hence an absolute URL:
scheme + hostname + absolute pathname
<a href="https://www.rte.ie/news/index.html">RTE News</a>
You want a link in siamese.html
to guppies.html
An internal link, hence three options.
either
<a href="http://www.example.org/fish/guppies.html">Click me</a>
or
<a href="/fish/guppies.html">Click me</a>
(N.B. The forward slash at the start.)
or
<a href="../fish/guppies.html">Click me</a>
(N.B. No forward slash at the start; no scheme, no host.)
Some other common directives.
2XX | Success | e.g. 200 OK |
3XX | Redirection | e.g. 301 Moved Permanently |
4XX | Client error |
e.g. 400 Bad Request e.g. 403 Forbidden e.g. 404 Not Found |
5XX | Server error | e.g. 500 Internal Server Error |
For client and server errors, some web developers like to serve customized error pages.
To do this, use an ErrorDocument
directive in httpd.conf
<!DOCTYPE html>
<html>
<head>
<title>404 error</title>
</head>
<body>
<p>
Oops!
</p>
</body>
</html>
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
ErrorDocument 404 /errors/bad_url.html
If a browser requests a file, the server serves that file.
But what does a server do if a browser requests a directory?
E.g. http://www.example.org/fish/
If a browser requests a directory, one option is for the server to return a default page.
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
DirectoryIndex index.html
If this is your httpd.conf
, then Apache will serve you a file called index.html
from the directory you requested.
Another option is for the server to return a page that lists the contents of the directory.
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
<Directory />
Options +Indexes
</Directory>
Here, Apache will serve you a list of the contents of the directory that you requested.
Be wary of setting things up in this way!
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
DirectoryIndex index.html
<Directory />
Options +Indexes
</Directory>
request |
---|
http://www.example.org/ |
response |
/var/www/html/index.html |
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
DirectoryIndex index.html
<Directory />
Options +Indexes
</Directory>
request |
---|
http://www.example.org/fish/ |
response |
a listing of the contents of fish |
ServerName www.example.org
Listen 80
DocumentRoot "/var/www/html"
DirectoryIndex index.html
<Directory />
Options +Indexes
</Directory>
request |
---|
http://www.example.org/cats/ |
response |
/var/www/html/cats/index.html |