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.