3

Using a framework makes it easy to list full url's for my html src and href attributes, and I feel I'm being more thorough by listing a full url instead of a relative path. But is this faster? Am I incurring an extra DNS lookup? What is the best practice when the content is on the same server?

<img src='http://site.com/images/img1.png' />

vs

<img src='/images/img1.png' />

Codeigniter's image helper img() works like this from the users' guide:

echo img('images/picture.jpg');
// gives <img src="http://site.com/images/picture.jpg" />

and Codeigniter's anchor helper anchor() works like this from the users guide:

echo anchor('news/local/123','My News');
// gives <a href="http://example.com/index.php/news/local/123" >My News</a>
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
steampowered
  • 11,809
  • 12
  • 78
  • 98
  • 1
    I develop on a remote server, so the http lookup always works. I synchronize my local ide with the remote server on each save, and I only run the code on the remote server. The "http://mysite.com/" part is generated by a method in my framework, so it's easy to change if I switch domains. Nobody answered my question about DNS lookup speed, which was my pain purpose for asking. – steampowered Sep 21 '11 at 20:35
  • I edited my question to include the codigniter stuff, and also tagged codeigniter – steampowered Sep 22 '11 at 00:55

4 Answers4

6

As far as DNS goes, it really doesn't matter if you have relative or absolute URL. Your browser ends up pre-pending the server URI onto the front anyway. Also, your network stack does the lookup for the first time, and caches the IP. Unless something goes wrong, there should only be the one lookup per page. YMMV of course, but that should be how this all works.

uotonyh
  • 786
  • 5
  • 7
4

'Never' (alsmost never) use absolute paths.

It will bite you in the ass later.

For example when you switch / add another domain.

Go from your test to production server.

Basically the rule is internal URL's should be relative.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I recently bought a premium script which was using full url in href like this and have defined $basehttp = "http://www.mydomain.com"; in the config file , so changing the domain was very easy , but its still unclear which one to use. i am thinking from the pagespeed POV. –  Jan 01 '14 at 04:09
1

Oh you really don't want to use a full path. You'll have a lot of work ahead of you:

  • If you want to develop the site locally
  • You change / add domains (development, staging, etc)
  • You switch to using a CDN

You also will break your dev environment, since most modern ones will perform local directory lookups. Can't do that with a domain.

Also, in a dev environment you will be pulling from the production site, which will make modifying and adding images extremely tricky.

Most importantly, other developers working with your code will try to kill you. And that's bad for your health.

Mike Robinson
  • 24,971
  • 8
  • 61
  • 83
  • Most frameworks allow you to define a variable for the domain, so it's easy to change the entire site domain with one line of code. All that is needed to make it local is add the "localhost/" to the one line of code. – steampowered Sep 29 '11 at 20:05
0

Portability would be the issue for me. I would choose the second option based on that alone.

mothmonsterman
  • 2,451
  • 3
  • 21
  • 28