Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

While I know the back button does this perfectly well, I was wondering if there was a good way to "return to" a paginated page using a previous request or something? Wanting to do this server-side rather than relying on JS. Thanks.

share|improve this question
Can you elaborate a bit? E.g. are you linking from a paginated entry listing to an entry's actual URL? – Brandon Kelly Jun 30 '14 at 20:28
Precisely. A blog list view that is paginated. Say you're on pg 5 and choose an entry I want to go back to pg 5 with a text link on entry view versus just the blog landing page. Wasn't sure if it was in the request or session arrays or not. If not, I will make a small plugin. – Ryan Shrum Jun 30 '14 at 20:47
up vote 4 down vote accepted

The easiest way would be to check craft.request.urlReferrer and see if they just came from the entry index page. If so, provide a link to send them back.

{% set referrer = craft.request.urlReferrer %}
{% set indexBaseUrl = url('articles') %}

{% if referrer and referrer|slice(0, indexBaseUrl|length) == indexBaseUrl %}
    <a href="{{ referrer }}">Go back</a>
{% endif %}

Another, more explicit way to do it would be to actually pass the current page in the query string of the entry URLs, from the entry index page.

{% for entry in entries %}
    {% set url = url(entry.uri, { page: craft.request.pageNum }) %}
    <a href="{{ url }}">{{ entry.title }}</a>
{% endfor %}

Then you could check for that “page” query string param in the single entry template:

{% set indexPage = craft.request.getQuery('page') %}
{% if indexPage %}
    {% set indexUrl = url('articles/' ~ craft.config.pageTrigger ~ indexPage) %}
    <a href="{{ indexUrl }}">Go back</a>
{% endif %}
share|improve this answer

This is not something built into Craft, but you could accomplish this with a plugin. You could have it run during your plugin's init function. On each page load, it would take the current page and load it into a currentUrl session variable. Then when you went to another page, the previous page url would move from currentUrl to a prevUrl session variable, and the current one would go into currentUrl.

You would write a template variable to check and output them in your templates. You could possibly work with Craft's session flashes, but you would have to remember to tell it not to delete them when you grabbed them.

share|improve this answer

I'd guess if this existed it would be available through craft.session. Unfortunately, I don't see any reference to a variable for the previous page's URL in the documentation for craft.session:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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