craft.request.urlReferrer returns the full URL of the referrer, while craft.entries.uri accepts the URI only.

Do I need to split the URL just to get URI path, write a twig filter, a custom plugin?

Maybe I can’t see the forest for the trees, but it seems like there should be an easier way.

  • I don't see how you can query an entry for URLs which link to an entry (en.wikipedia.org/wiki/HTTP_referer) Are you looking for craft.request.url ? What are you trying to accomplish? – Moto_Nomad Sep 1 '16 at 22:54
  • My use case is fairly convoluted, but ultimately, I'm spoofing the "active" page in my navigation to be the referring page to the entry. It's not mission critical, but a nice consistency feature for the users. – kotyy Sep 2 '16 at 16:35

No obvious solution sticks out to me using only native Twig functions/filters.

It'd be pretty trivial to write a custom filter using parse_url(), though...

  • Thats not a super helpful answer. – Paul Danelli Sep 18 '20 at 15:16
  • Seems like it was helpful enough to nudge the thread towards a nice solution... :-) – Michael Rog Oct 12 '20 at 23:48

I ended up writing a custom Twig filter, per Michael's suggestion.

namespace Craft;

use Twig_Extension;
use Twig_Filter_Method;

class BusinessLogicTwigExtension extends \Twig_Extension
    public function getFilters()
        return array(
            'pathFromUrl' => new \Twig_Filter_Method($this, 'pathFromUrl')

    public function getName() {
        return Craft::t('Path From URL Extension');

    public function pathFromUrl($url)
        return ltrim(parse_url($url, PHP_URL_PATH), '/');

Add that to a plugin, and add the addTwigExtension method to your main plugin file, for example:

public function addTwigExtension() {
    return new BusinessLogicTwigExtension();

You can then use the filter with craft.entries.uri and craft.request.urlReferrer to obtain the entry for the referring page.

{% set referringEntry = craft.entries.uri(craft.request.urlReferrer|pathFromUrl).first() %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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