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"></img>

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.)

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 LindalJun 21 '09 at 13:51

If you have lots of links on a page using absolute paths creates a slight (probably negligible) increase in bandwidth used.
–
M. DudleyJun 21 '09 at 13:53

See - I told you I would be corrected - thanks emddudley.
–
ChrisF♦Jun 21 '09 at 14:01

@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♦Jun 21 '09 at 14:03

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 LindalJun 21 '09 at 14:08

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.

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.

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.

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.

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.