Wednesday, January 20, 2010

Section 4.11. What's an Absolute Path?










4.11. What's an Absolute Path?


The last time we talked about paths we were writing HTML to make links with the <a> element. The path we're going to look at now is the absolute path
part of a URL, the last part that comes after the protocol (http) and the Web site name (www.starbuzzcoffee.com).


An absolute path tells the server how to get from your root folder to a particular page or file. Take Earl's Autos
site, for example. Say you want to look in Earl's inventory to see if your new Mini Cooper has come in. To do that, you'll need to figure out the absolute path to the file "inventory.html" that is in the "new" folder. All you have to do is trace through the folders, starting at the root, to get to the "new" folder where his "inventory.html" file is located. The path is made up of all the folders you go through to get there.


So, that looks like root (we represent root with a "/"), "cars", "new", and finally, the file itself, "inventory.html". Here's how you put that all together:



earls_autosEarl's Autos root folder.
"/"Always start at the root.
cars
/
carsAdd on each folder as you navigate to the file.
newIn between the folder names, put a "/" to keep them separate.
/
new
inventory.html
used
inventory.html
inventory.html
And then add on the filename.
/cars/new/inventory.html
Absolute path to "inventory.html".




4.11.1. there are no Dumb Questions


Q:

What is important about the absolute path?

A:

The absolute path is what a server needs to locate the file you are requesting. If the server didn't have an absolute path, it wouldn't know where to look.

Q:

I feel like I understood the pieces (protocols, servers, Web sites, and absolute paths), but I'm having trouble connecting them.

A:

If you add all those things together you have a URL, and with a URL you can ask a browser to retrieve a page (or other kinds of resources) from the Web. How? The protocol part tells the browser the method it should use to retrieve the resource (in most cases, this is HTTP). The Web site part (which consists of the server name and the domain name) tells the browser which computer on the Internet to get the resource from. And the absolute path tells the server what page you're after.

Q:

We learned to put relative paths in the href attribute of our <a> elements. How can the server find those links if they aren't absolute?

A:

Wow, great question. When you click on a link that is relative, behind the scenes the browser creates an absolute path out of that relative path and the path of the page that you click on. So, all the Web server ever sees are absolute paths, thanks to your browser.

Q:

Would it help the browser if I put absolute paths in my HTML?

A:

Ah, another good question, but hold that thought, we'll get back to that in a sec.



Sharpen your pencil


You've waited long enough. It's time to give your new URL a spin. Before you do, fill in the blanks below and then type in the URL (like you haven't already). If you're having any problems, this is the time to work with your hosting company to get things sorted out. If you haven't set up an hosting company, fill in the blanks for www.starbuzzcoffee.com, and type the URL into your browser anyway.


protocol Web site name absolute path




I'd like my visitors to be able to type "http://www.starbuzzcoffee.com" and not have to type the "index.html". Is there a way to do that?


Yes, there is. One thing we haven't talked about is what happens if a browser asks for a directory rather than a file from a Web server. For instance, a browser might ask for:



Remember, when we're talking about Web servers or FTP, we usually use the term "directory"
instead of "folder." But they're really the same thing.

http://www.starbuzzcoffee.com/images/
The images
directory in the root directory.

or
http://www.starbuzzcoffee.com/The root directory itself.
But, you need to find out what your hosting company wants you to name your default file,
because it depends on the type of server they use.




When a Web server receives a request like this, it tries to locate a default file in that directory. Typically a default file is called "index.html" or "default.htm" and if the server finds one of these files, it returns the file to the browser to display.


So, to return a file by default from your root directory (or any other directory), just name the file "index.html" or "default.htm".


But I asked about "http://www.starbuzzcoffee.com", which looks a little different. It doesn't have the ending "/".


Oops, you sure did. When a server receives a request like yours without the trailing "/" and there is a directory with that name, then the server will add a trailing slash for you. So if the server gets a request for:



http://www.starbuzzcoffee.com/
it will change it to:
http://www.starbuzzcoffee.com
which will cause the server to look for a default file, and in
the end it will return the file as if you'd originally typed:

http://www.starbuzzcoffee.com/index.html














No comments: