Direct link to routed url


I was wondering if there was a way to link directly to a routed page. I followed the doc example with a cleaner url domain/blog/blog-post-title routed to domain/blog-post-title and when I created a link to this page with url() obviously it link the “real” url, and it’s redirected afterward.

I’m not sure this is a good think for visitors and SEO. So for the moment I construct my links with $site->url().$page->uid() but I’m not sure it’s bullet proof nor it’s a good method.

I would like to have your opinion ont this, is my method is valid, and to know if a method exists to point to the routed url (I don’t think so as the routing is done on the flight)

Last question: The routing is a 301 redirect or another kind of redirect? (in cas I forgot to reshape some URLs)

Thank you!

Yes, you have to set the URL manually, but you could, for example, use a page model to create the correct URL.

As regards the redirect code, I’m not sure, but your dev tools should answer this question.

Thanks. I tried something like that before the manual construction : echo url(page($blog_post)->uri() but it threw an error with “uri on a bolean” so I gave up. What are the benefits of using the page method vs the “manual” construction?

Cleaner looking code :wink:

Another solution that will work out of the box without changes in the templates: Create a page model for the blog post pages and override the url() method. Please note that this will not work if your blog posts have any kind of child page.

1 Like

how did you do that ? Can you also give me an example of the page method url building ? I can’t figure what I did wrong. Thank you.

The page model (not page method!) would look like this:


class ArticlePage extends Page {
  function url() {
    return url($this->uid());
1 Like

Oops, of course I meant model, not method, corrected it above.

Did you know why the code above threw an error ? I have to call the page objet first ?

In your example, how the other pages know that url() refer to that in this case ? If I call blog_post->url() anywhere in my site / other templates, this gonna work ?

Thank you for your help

Models for a page work everywhere where that particular page is called.

Page method is correct as well, it’s just a different approach. So no worries. :slight_smile:

Well, yes, of course, but with a page method you can’t override an existing method, while you can with a page model.

1 Like