Are there benefits to using {site_url} rather than a relative path?

12

Should I use {site_url}? I prefer to use relative paths in my templates.

Example:

/resources/glossary/category/{category_url_title}

Rather than:

{site_url}/resources/glossary/category/{category_url_title}

I was wondering if anyone could provide an argument of why one should be used over the other? Is it a personal preference thing? Should I always use {site_url}? Should I never use {site_url}?

My reasoning for not using it is because I believe it takes less resources to use a relative path. Logically, one would think that because EE doesn't have to replace {site_url} with http://www.example.com in every template, the page would load faster?

I'm using FocusLab Master Config on all of my sites.

Kelly Cook

Posted 2013-03-22T13:37:04.413

Reputation: 428

Answers

13

In most cases, using relative paths are fine, though you won't see any performance gain there (replacing global variables like {site_url} is the tiniest of tasks).

But in the cases where you do want a full URL generated, {site_url} is not the best way to do it - instead, you should use {path=""}, {permalink=""}, {title_permalink=""}, etc. This way you'll never run into issues with slashed/unslashed URLs, and it's much cleaner.

So when would you want to use {path}? If you have some parts of your site which run under SSL, this will prevent all of your nav links, etc, from being linked to with an https URL when leaving those SSL-protected areas.

Derek Hogue

Posted 2013-03-22T13:37:04.413

Reputation: 17 344

1I was about to mention the direct path causing problems if someone's viewing your site under https, but you caught that one... – None – 2013-03-22T17:27:38.810

I have accepted this as the correct answer because it is the best answer out of the ones received. Thank you for your insight to the path variable...I haven't used that one very much. – Kelly Cook – 2013-03-23T20:28:15.843

5

I would like to point out that your first example is not a relative path - it's an absolute path with a variable domain. This crucial difference explains why the full path in your second example is the preferred form.

If you have a page at example.com/foo.html, and a page at example.com/bar/baz.html, there's a few ways to link to the second page from the first:

  • Absolute path with site_url: { site_url }/bar/baz.html
  • Absolute path without site_url: /bar/baz.html
  • Relative path: bar/baz.html

In this case, it's pretty straightforward. Now imagine the reverse - how exactly will baz.html link back to foo.html?

  • Absolute path with site_url: { site_url }/foo.html
  • Absolute path without site_url: /foo.html
  • Relative path: ../foo.html

The relative paths for the pages have an entirely different format, because they're relative to where the linking page is. This is bad, it means your URL formatters have to be context-sensitive. So that's out.

So either form of absolute path looks good, right? Not quite. Imagine you wrote your code on a server that gives you the subdomain user.example.com, and you used the second form, that doesn't use site_url, because it was simplest at the time. Everything will work perfectly.

Then, later, you have to migrate to a server that puts your account at the url example.com/user/.

Well, crap. Now all your links are going to go to example.com/foo.html instead of the required example.com/user/foo.html. You can go through and update every single reference, but that's unnecessary work.

If you'd written them using { site_url } from the start, you only have to change that one setting.

Izkata

Posted 2013-03-22T13:37:04.413

Reputation: 159

May I ask why I got the downvote? (I got here from the Hot Questions list, so sorry if I missed something specific to this SE site - but my answer valid for web development in general) – Izkata – 2013-03-22T19:02:42.117

I'd imagine that the downvote is for the fact that the answer demonstrates a lack of understanding as to how EE works. But I think your real point - that if you move your root to a subfolder, your links will all break - is a good one. – Derek Hogue – 2013-03-22T19:37:28.773

@DerekHogue Ah, heh. No complaints there - never used EE before. Just had done a quick Google search to be sure it was what I thought it was. – Izkata – 2013-03-22T20:39:53.233

+1 on issues moving to a subfolder, especially during development. – Adrian Macneil – 2013-03-23T02:45:47.530

3

Speed wise I would go for the relative paths (less DNS lookups) Also for java and css (minified) I would use relative paths. (major speed increase)

So basically if you are running one website with EE, I would go for relative paths. Speed increase is not due to the fact that EE needs to enter the domain in every {site_url} tag. it's due to the DNS lookups/requests.

Gfive

Posted 2013-03-22T13:37:04.413

Reputation: 66

1As far as I know, the existence of a domain has no effect on if a browser will perform a DNS lookup or not - it performs a lookup if referencing a domain it hasn't yet already looked up. A relative path and a domain that matches the current URL should effectively be the same thing from a DNS lookup point of view. – John D Wells – 2013-03-22T17:10:32.093

1One benefit of using relative URIs is the convenience of not having to have to deal with $config variables to handle multiple development environments. Other than that, they're pretty identical in function. Using relative URIs can also remove the worry of www vs non-www matches; but technically you should force one or the other via htaccess or similar. – Jarrett Barnett – 2013-03-22T19:04:29.177

Browsers and your OS cache DNS requests, so this argument is void. – Adrian Macneil – 2013-03-23T02:42:13.127