absolute vs relative links – and gzip compression

Yesterday I saw a tweet, about if an internal link should contain the domain name or not, so be absolute or relative.
The reply I saw, was that using the domain name, could prevent people from ‘copying’ the text directly, because it would contain the link for the original domain. Which is true. But on other hand, using without the full domain (relative) it would speed up the site, because the amount of data being sent would be less.

The above statement is true, however, if a site is using gzip compression, it doesn’t really matter if you use the full domain or not, because the size doesn’t really change that much.

To test this, I created 2 files, each one containing 1000 links, so file1 contained relative links like:

<a href="/js/jquery1.js"></a>
<a href="/js/jquery2.js"></a>
<a href="/js/jquery999.js"></a>
<a href="/js/jquery1000.js"></a>

file2 contained absolute links like:

<a href="http://domain.com/js/jquery1.js"></a>
<a href="http://domain.com/js/jquery2.js"></a>
<a href="http://domain.com/js/jquery999.js"></a>
<a href="http://domain.com/js/jquery1000.js"></a>

So we added http://domain.com to each link, which is 17 bytes to each link – or 17000bytes (~17KB).

File1 had a filesize of 30,893 bytes and file2 had a filesize of 47,893 bytes

As you can see, 17000 bytes difference. This is indeed a difference, when talking about sending data via the internet, so the statement is true, that it might make the site load faster, especially on really slow connections.

However, if gzip compression is enabled, so the html document is compressed, it doesn’t make a big difference when talking about data transfer.

Because when gzipping file1 it had a filesize of 2454 bytes, or around 2.4KB, but file2 had a size of 2625 bytes or 2.6KB – which is 0.2KB difference in size.

So it doesn’t really matter if you use absolute or relative links, when building a website, because it’s not really a good optimization if gzip compression is already enabled.

So how do you enable gzip compression? – Most hosts that use cPanel have a tool in the CP called ‘Optimize website’ which makes it possible with a single click to enable gzip compression. But for those who don’t use cPanel, you can place the following code in your .htaccess

<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html

        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary