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.

3 Answers 3

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.

2
  • 1
    I was about to mention the direct path causing problems if someone's viewing your site under https, but you caught that one...
    – user800
    Commented Mar 22, 2013 at 17:27
  • 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
    Commented Mar 23, 2013 at 20:28
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.

4
  • 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
    Commented Mar 22, 2013 at 19:02
  • 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. Commented Mar 22, 2013 at 19:37
  • @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
    Commented Mar 22, 2013 at 20:39
  • +1 on issues moving to a subfolder, especially during development. Commented Mar 23, 2013 at 2:45
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.

3
  • 1
    As 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. Commented Mar 22, 2013 at 17:10
  • 1
    One 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. Commented Mar 22, 2013 at 19:04
  • Browsers and your OS cache DNS requests, so this argument is void. Commented Mar 23, 2013 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.