503 individual pages in Craft CMS

By Matt,

February 2016

Web Dev
If you have any level of SEO juice in your can, it's always worthwhile preserving it. We’ve recently made a few updates to the site and are re-writing and restructuring some of our existing case studies.

Rather than wait until all of the content was re-written, we wanted to temporarily remove the pages from the site. The correct way to do this from an HTTP code perspective is to return a 503 Error Code. This lets the browser know that the page is undergoing maintenance and will return after some delay.

We use Craft CMSfor our website. If you set Craft’s Service Status to Off, it will return a 503 to all pages. That definitely wasn’t what we needed!

After a bit of googling, we found it was very easy to configure any individual page to return a 503 error by adding a few lines of code to your template:

{# your/dir/_entry.twig #}

{% if entry.slug == "your-page-slug" %}
    {% exit 503 %} {# FYI - you can return any HTTP code here #}

{% endif %}

This will basically stop the template from executing and tell Craft to return a 503 error. Here are the docs on the {% exit %} tag.

You can then add a custom 503.twig template into your templates root directory and Craft will display this for all 503s.