2

A coworker of mine asked me if I could make the links in Craft relative.

Which is not a big deal for navigation links,

relative url:

{% set relativeUrl = entry.url|replace((siteUrl), '/') %}

but it gets trickier for links inside a Rich Text field.

Craft is set up to use absolute links, is there a reason behind that?

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Marco Schuler
  • 620
  • 3
  • 13

2 Answers2

3

I would question the need to have relative URLs to begin with.

As Brad mentions, Craft neatly solves the multi-environment problem for you.

If you come from an environment like WordPress where it makes absolute URLs for you and then you end up having to do a find-replace on the content when switching environments (since there's no "built-in" way to do it), I can see where your co-worker might think relative is the way to go. However, even WP has good reason to do so.

Back before content management systems, relative URLs were probably the way to go because they are easier to code by hand; if you used conventions like "../" your pages also worked locally. Now with systems like Craft and local testing servers, creating links within content is much more consistent.

Craft even has a find/replace tool in the CP if you want to make sure those staging/production URLs absolutely get switched over in the database as well.

Speaking of consistency, google wants you to be as efficient as possible. From an SEO perspective, this moz post goes over the pros and cons of relative vs absolute but a few points worth summarizing:

  • Scrapers can be a big problem stealing your content. Absolute URLs make it a bit more difficult for the laziest of them.

  • By using things like rel="canonical" and letting Craft spit out your main base URL consistently, you can help avoid duplicate content issues.

Yoast gives some thoughts on this as well but I'll leave you with one takeaway:

The few minutes a developer might save by using relative URLs are offset by countless hours an SEO might be spending to solve the issues caused.

RitterKnight
  • 6,582
  • 14
  • 24
  • so you don't answer the question, reject the premise and reason with an article that basically states "it's bad for seo if you do it wrong" and present that as an answer to the question you rejected. – scones Jul 27 '21 at 12:56
  • @scones: Sorry you feel this didn't answer the question but clearly the OP thought it was since it was marked as the accepted answer. Feel free to write another answer yourself if you have another opinion. – RitterKnight Jul 27 '21 at 15:57
1

Behind the scenes, the Rich Text field converts those links to reference tags so that when the Craft install moves between environments (and the URLs change) or, for example, the entry's slug changes, it will still maintain a proper link to the entry instead of breaking. Currently it requires linked URLs in absolute format to do that.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • my rich text fields don’t seem to be converting links to reference tags, so when moving between environments, all of my rich text field links break. Is there a config setting for this? my question – artmem Mar 11 '18 at 14:59