Google Is Not Immune To Problems Obama is the 44th President of The USA

PHP Speed Hack - Root URL's

There is nothing wrong with relative vs. absolute URL’s. This is covered in the HTTP spec (s3.2.1) and a lot of codebases design with relative url’s. The code looks like this generally:

<a href="/images/something.gif"/>
<a href="/blog/category/entry">Entry</a>

This could be done a bit better, as you may find that your site slows down quickly as it gets loaded. Two rules can apply to making this part of your code better.

First, you will want to serve up media eventually from a different host so that your users browsers can load multiple resources concurrently, rather then a request/response/request/response model that a single server will accommodate. (Although pipelining is defined in HTTP/1.1 it is not used in IE, webkit etc so the majority of browsers will not be making 20 requests all at once to your servers).

Second, Caching applications are more likely to work cleanly with your app with the link structure designed around absolute links. Relative URL’s aren’t bad per se, I have just found that absolute links in your content give you an upper hand in the crucial moments when you might need to do caching or load-balancing because your site is suffering The Slashdot Effect.

Note that this isn’t a true speed change as the render time will be limited by the clients browser, however this will significantly improve the apparent speed as data can reach the browser quicker within the bandwidth constraint. As a rule, this will help you out a lot in the long run. Here is how I do this in php. I set up constants somewhere in a global config file:

define('IMAGE_BASE', 'http://yourimagehost.com/images/');
define('WEB_ROOT', 'http://yourwebsite.com/');

Then in my code, I simply add this (inline perl is great for making these changes):

<a href="<?=IMAGE_BASE;?>something.gif"/>
<a href="<?=IMAGE_BASE;?>blog/category/entry">Entry</a>

Yahoo calls this a Content Delivery Network. When you need to move your media to another server like AWS, Akamai etc or you need to load-balance the media across multiple machines, you can set the constant at the beginning of the page render either from a pool of hosts, a network latency measurement or simply a single external host.

I hope this helps you prepare your code for those high traffic times :)

**Update 1: ** Connectivity Enhancements in Internet Explorer 8

With Internet Explorer 8, the maximum number of concurrent connections from a single host process connecting via broadband to a single server has been increased to 6. In Internet Explorer 7 and earlier, the maximum number of concurrent connections per host process to a single server via HTTP 1.1 is 2. For HTTP 1.0, the limit is 4, though HTTP 1.1 connections are far more common today. Note that the maximum number of concurrent connections from a single host process connecting via dial-up (with a modem over a telephone line) to a single server remains the same as for Internet Explorer 7 and earlier.


About this entry