Take the 2-minute tour ×
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.

I'd like to make my 404 page editable through Craft, but when I create a single for it, a URI is required.

Is there a way to make a single with no URI, or is the best route to setup globals for managing this content? Or is there a better way that I don't know about?

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

I would create a Single with the URI set to "404", and also set its Template to "404". That entry will automatically get loaded if you go to http://example.com/404, and load your 404.html template, so at the top of the template you should add this to it:

{#
 #  Fetch the 404 Single entry, if we don’t already have it.
 #  (requests to /404 will already have it.)
 #}
{% if entry is not defined %}
    {% set entry = craft.entries.uri('404').first() %}
{% endif %}
share|improve this answer
    
That works pretty well, only problem is if you hit /404 it doesn't have the 404 header. Not the end of the world, but would be nice. –  Jeremy Gimbel Jun 21 at 21:28
1  
You could add a redirect to a non-existing page (e.g. 404-not-found), if last segment is 404. Not a clean solution, but you'd get a 404 header ;) –  Victor In Jun 21 at 21:30
1  
@JeremyGimbel That was already the case, and it’s pretty common behavior (if you’re trying to directly access the 404 page, and you get the 404 page, you technically got exactly what you wanted). See github.com/404 for example. If you really don’t like that, you could add this before that conditional: {% if entry is defined %}{% exit 404 %}{% endif %} –  Brandon Kelly Jun 21 at 21:33
    
Fair enough. Thanks! –  Jeremy Gimbel Jun 21 at 21:42
add comment

404 redirect:

You could set up your single to use 404-not-found (example) as a uri, and add a redirect:

{% redirect '/404-not-found' %}

to the /404.html file. That way every 404-page gets redirected to the single.

Downside: You will loose the 404-header, and the uri in the address-bar will be lost.

.

Load Single:

Set up a single, and load the single entry into the /404.html file:

{% set entry = craft.section('404-single-handle').first() %}

Now you can use the entry-var just like you would in a normal single template.

You will have to use a single-uri, but you could add a conditional to the single template (not the 404.html), which redirects to index if it gets accessed directly:

{% redirect '/' %}

.

EDIT: I didn't know a single can be set to 404 without breaking the functionality, so Brandon's answer is the way to go ;)

share|improve this answer
add comment

Your Answer

 
discard

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.