Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

splatEric's avatar

Relative vs Absolute URLs

For reasons that are not really relevant to my question, I was wondering why the default behaviour of the route helper is to generate absolute URLs. The most common use case I have for using the helper is to generate form action routes and links. Having these links without the domain in them would seem far more logical than with. It also means that any views that are cached are only useable in the one domain context (and are vulnerable to host header injections if you're serving the application naively)

So my questions are really:

  1. Am I missing another helper that I should be using for forms and links, or is it common practice to just explicitly make calls to route with the $absoluteUrl flag set to false?
  2. Am I missing something obvious as to why its better for links etc to incorporate the domain in the view files as the default implies?

Appreciate any thoughts or insights people might have on this.

0 likes
5 replies
bait-dept's avatar

As per the docs, $url = route('route.name', ['id' => 1], false); wol generate a relative route.

As far as relative vs absolute, once upon a time it was better for seo because it wasnt as good as today. I dont think thats still the case but I always prefer absolute.

kokoshneta's avatar

@imrodrigoalves The FALSE parameter is mentioned in the question; that’s beside the point. Assuming you have your server and your links properly set up, relative links work just as well for SEO as absolute links and have for many, many years.

I too have found it a bit of an annoyance to have to include three parameters to every route() call. And it’s glaringly inconsistent that Storage::url() will return a relative URL (if using the local driver, of course) and doesn’t even offer the option to return an absolute URL.

JamesZimmerman's avatar

First of all you have to define the terminology. An url without schema and domain but started from a slash is still perfectly absolute (as opposed to relative url that does not start from a slash). So, we have to keep in mind these two kinds of absolute urls.

You should only ever use relative URLs for quick and dirty pages, because the URLs will break quickly as soon as you have directories in the path.

Full absolute URLs have the advantage that if somebody is scraping your content (in a way that you want them to do) then it will still work without them doing anything. For example, RSS readers. Also, if you have links in the content, then whenever it is scraped they will all link back to your original content (good for SEO).

But relative URLs are more flexible and convenient. Ideally, use a router which will generate absolute URLs that are correct for the host on which the omegle.2yu.co PHP is being executed, but it's probably not a big deal if you use the short https://omegle.love URLs.

splatEric's avatar

@JamesZimmerman that is an excellent clarification of terms in the general context, and I was in fact distinguishing between full and absolute URLs per your description.

I used the absolute url term because that is the name of the flag that is passed to the route helper.

It is the full URL that is causing me pain, and is a cause for concern in any view caching that might be performed. It may also be causing me trouble in redirects, where I can’t find a way to get the standard Redirector to not use a full url.

PeteBatin's avatar

@JamesZimmerman sorry, I know an old thread. I was searching Google on how to obtain an absolute URL and as a result of this post now know that route() is absolute unless set to false.

Just wanted to point out that relative URLS aren't for quick and dirty pages and they will not break if you have directories in the path, it's just that the document has not been setup correctly. You need to set your base tag https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/base

<base href="https<colon>//somedomainname<dot>com">

All relative links will then work from the base href attribute (and not a directory).

Please or to participate in this conversation.