<p>I ended up writing a custom Twig filter, per Michael's suggestion.</p>

<pre><code>&lt;?php
namespace Craft;

use Twig_Extension;
use Twig_Filter_Method;

class BusinessLogicTwigExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'pathFromUrl' =&gt; 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), '/');
    }
}
</code></pre>

<p>Add that to a plugin, and add the <code>addTwigExtension</code> method to your main plugin file, for example:</p>

<pre><code>public function addTwigExtension() {
    Craft::import('plugins.businesslogic.twigextensions.BusinessLogicTwigExtension');
    return new BusinessLogicTwigExtension();
}
</code></pre>

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

<pre><code>{% set referringEntry = craft.entries.uri(craft.request.urlReferrer|pathFromUrl).first() %}
</code></pre>

CC BY-SA 3.0
2016-09-02T16:32:46.843Z
2016-09-02T16:32:46.843Z
kotyy
<p>No obvious solution sticks out to me using only native Twig functions/filters.</p>

<p>It'd be pretty trivial to write a custom filter using <a href="http://php.net/manual/en/function.parse-url.php" rel="nofollow"><code>parse_url()</code></a>, though...</p>

CC BY-SA 4.0
2020-09-18T15:16:02.373Z
Thats not a super helpful answer.
Paul Danelli
CC BY-SA 4.0
2020-10-12T23:48:09.430Z
Seems like it was helpful enough to nudge the thread towards a [nice solution](https://craftcms.stackexchange.com/a/16665/689)...  :-)
Michael Rog
CC BY-SA 3.0
2016-09-01T23:19:43.423Z
2016-09-01T23:19:43.423Z
Michael Rog
<p><code>craft.request.urlReferrer</code> returns the full URL of the referrer, while <code>craft.entries.uri</code> accepts the URI only. </p>

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

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

CC BY-SA 3.0
2016-09-01T22:54:13.230Z
I don't see how you can query an entry for URLs which link to an entry (https://en.wikipedia.org/wiki/HTTP_referer)  Are you looking for craft.request.url ?   What are you trying to accomplish?
Moto_Nomad
CC BY-SA 3.0
2016-09-02T16:35:03.237Z
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
<entries>
Querying the entry for an HTTP Referer
# Querying the entry for an HTTP Referer

(Asked by: username_0 on 2016-09-01)

`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.

**Tags:** entries
> **Comments:**
> **username_1**: I don't see how you can query an entry for URLs which link to an entry (https://en.wikipedia.org/wiki/HTTP_referer)  Are you looking for craft.request.url ?   What are you trying to accomplish?  
> **username_0**: 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.  

---

## 2 answers

### Answer by username_0 on 2016-09-02. Score: 1 | Accepted answer

I ended up writing a custom Twig filter, per Michael's suggestion.
 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() {
    Craft::import('plugins.businesslogic.twigextensions.BusinessLogicTwigExtension');
    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() %}


---

### Answer by username_2 on 2016-09-01. Score: 2

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()`](http://php.net/manual/en/function.parse-url.php), though...

> **Comments:**
> **username_3**: Thats not a super helpful answer.  
> **username_2**: Seems like it was helpful enough to nudge the thread towards a [nice solution](https://craftcms.stackexchange.com/a/16665/689)...  :-)  

---

