views:

234

answers:

7

Because of some apache rewrite rules in a project I'm working on, it's convenient to make href's and links in general absolute, because that assures that the browser will find the file behind that link, no matter what the URL is.

Example:

<img src="http://localhost/project/gfx/abc.jpg"&gt;&lt;/img&gt;

instead of

<img src="gfx/abc.jpg"></img>

Would the former be slower than the latter, or in any way not as good?

It's not like I think of this as optimization; I guess I will use the absolute ones anyways (unless there's a really good reason not to do), but I'm interested in knowing whether using absolute URL's are OK or not.

Note that this will apply to all links/paths (hyperlinks, css and javascript includes, AJAX-calls etc.)

+7  A: 

Relative paths are good for a variety of reasons a lot of which boil down to:

  • you can move the whole site to a new domain and not have to modify every single file.

This doesn't just apply to changing domains if the site is live, but when you deploy the site too moving from test to production. If you had absolute paths you'd have to change all references from:

<a href="http://localhost/..."&gt;...&lt;/a&gt;

to:

<a href="http://www.truedomain.com/..."&gt;...&lt;/a&gt;

I don't think there's any performance impact - but I'm sure others more knowledgeable than me will soon correct me if I'm wrong.

ChrisF
My absolute paths are generated server side, so I'll don't think that would make a difference (change a few variables, and they should be OK)?
Yngve Sneen Lindal
If you have lots of links on a page using absolute paths creates a slight (probably negligible) increase in bandwidth used.
emddudley
See - I told you I would be corrected - thanks emddudley.
ChrisF
@Yngve - you still have to change all the places the paths are generated. Also you have to change the code before it can be deployed, but once it's changed you can't test it locally, so technically you are deploying untested code. Does that make sense?
ChrisF
That means 1 line of code (global var), but I see what you mean. Then Samir's suggestion would be a good way to go I assume (eliminates the testing problem).
Yngve Sneen Lindal
+11  A: 

It won't make a bit of difference. You might prefer to do this though:

<img src="/project/gfx/abc.jpg" />

The browser will figure out the domain for you.

Samir Talwar
Root-relative URLs are definitely the most portable and easiest to maintain.
molf
A: 

Relative URLs use less bandwidth :P

But you should really be sold by ChrisF and Samir's answers. If you can't trust your Apache server not to rewrite things incorrectly, I'd suggest you look at solving that rather than painting over everything with absolutes.

Cogsy
+1  A: 

The browser will figure out the complete URL for any URL and reuse connections wheen possible, it needs the complete URL to request the file anyway.

Consider using an absolute URL without the domain name (as Samir suggested). If you use complete URLs with different domain names for the same site (for example www.mysite.com and mysite.com), the browser won't be able to reuse the connection.

Guffa
A: 

i would recommend the "/path/from/document_root" format. the only advantage to generating them dynamically as you mention in your comment is if you move your media to some sort of CDN with a different domain/IP.

nategood
A: 

AFAIK, either using absolute or relative paths should have the same performance on browser rendering. Each solution has its own advantages and disadvantages.

For instance, relative paths are better when you have to deploy the application on different domains and you don't have a way to automatically adapt the hostname. This problem doesn't actually exist if you manage your website with a server side programming language.

From the other side, absolute links has some kind of advantages. For example, many low-quality feed readers and parsers doesn't resolve the path to an asset when parsing you website or even your feed. Using absolute paths ensure a better compatibility with all kind of external parsers.

Simone Carletti
+1  A: 

The biggest problem is that you are not making your site "portable". Meaning you can move it from dev to staging to production without modifications. Have you thought about using the html base href tag instead? Then you only need to change it in one place.

There are variations on the base tag, like base target, that may also be useful. This is an old tag, so it would work in any browser.

Brent Baisley