Monday, April 17, 2006

HTTP protocol : absolute / relative urls

The question here is which one is better using absolute urls or using relative urls? Aim is to obtain better performance from the httpd server.

Performance wise there is no difference between an absolute and a relative url. Though relative urls need to be converted to absolute urls before they can be used. But who does the conversion? The client/browser or the http server. If the resolution is done at server end then use of absolute urls is better since extra processing required for conversion of relative to absolute would be avoided at the server end.

According to http://www.htmlhelp.com/faq/html/basics.html the browser resolves the relative URLs and not the server. Relative urls are resolved and converted to absolute urls before sending request to the server.

To quote exactly

Before the browser can use a relative URL, it must resolve the relative URL to produce an absolute URL. If the relative URL begins with a double slash (e.g., //www.htmlhelp.com/faq/html/), then it will inherit only the base URL's scheme. If the relative URL begins with a single slash (e.g., /faq/html/), then it will inherit the base URL's scheme and network location.

If the relative URL does not begin with a slash (e.g., all.html , ./all.html or ../html/), then it has a relative path and is resolved as follows.

1. The browser strips everything after the last slash in the base document's URL and appends the relative URL to the result.
2. Each "." segment is deleted (e.g., ./all.html is the same as all.html, and ./ refers to the current "directory" level in the URL hierarchy).
3. Each ".." segment moves up one level in the URL hierarchy; the ".." segment is removed, along with the segment that precedes it (e.g., foo/../all.html is the same as all.html, and ../ refers to the parent "directory" level in the URL hierarchy).

Please note that the browser resolves relative URLs, not the server. The server sees only the resulting absolute URL. Also, relative URLs navigate the URL hierarchy. The relationship (if any) between the URL hierarchy and the server's filesystem hierarchy is irrelevant.


From this following things can be derived:

1. In case of using relative urls, the processing for resolution of relative to absolute is done at client/browser end.
2. Relative urls are smaller to use and take up somewhat less bandwidth
3. Shifting documents from one server to another is easier when you use relative urls, since you dont need to go and change the urls in all the html documents.
4. The only disadvantage in using relative urls is that all content has to be on the same server. If you need to spread out the page content on different servers then you will need to use absolute urls.

This is what i got from the web...

All types of discussions on this are welcome.

No comments: