2

TL;DR

Is there a way to redirect with a status code and have the correct template load with the correct status code?


I have template code which looks like this:

{% if slug is defined %}
 {% set entry = craft.entries.section('foobar').slug(slug).first() %}
 {% if not entry.editable %}
    {% redirect 403 %}
 {% endif %}
{% endif %}

This works fine and redirects to the 403 template, but the problem is that is just redirects (changes the url) to /403 with a status code of 302

So I followed from this question and used the second parameter to set the status code like so:

{% redirect 403 403 %}

Which sets the status code fine but now my 403 template doesn't load. If I try it with 404 the same problem applies.

When I go to a 404 page which is handled by Craft (no redirect tag) then the status code is set correctly and my 404 template loads just fine.

Is there a way to redirect with a status code and have the correct template load with the correct status code?

2

{% exit 403 %} seems to be the answer! Seems so obvious now... facepalm

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.