Redirect to 404 When ExpressionEngine Entry has “Closed” Status

  • Twitter
  • Facebook
  • StumbleUpon
  • Reddit
  • Digg
  • Del.icio.us
  • E-mail
Posted May 5, 2011 by Ryan McLaughlin - nonactive Short URL: http://bcw.im/df78

Here's something interesting about ExpressionEngine's treatment of "Closed" entries: When an entry is set to the "Closed" status and you manually access the URL, you simply get your template rendered with no content in your {exp:channel:entries} tag.

Why wouldn't you want to have a 404 page being served when a user is trying to get to a Closed status? For example, let's say an entry is originally set as "Open" and, was indexed by Google. A few weeks later, the client decides to take down the page until further notice, so the entry's status is set to "Closed". However, Google obviously has the URL saved and served in their results, which directs some users to the broken template page with no explanation of what's going on. Instead, the page should show the visitor that the page is gone, and doesn't exist (Google will also remove sites from their listings if they crawl a 404'd page).

To make this happen, here's all that needs to be entered in your single entry page template:

{exp:channel:entries channel="my_channel" limit="1" status="closed"}
     {if status == "closed"}{redirect="404"}{/if}
{/exp:channel:entries}

This simply returns the closed entry if there is one, and redirects to your custom 404 page. Make sure to replace the channel with your own, and include the status attribute (by default, Closed entries aren't returned with the {exp:channel:entries} tag).

Update: In the comments Robson Sobral points out that the {no_results} tag can also accomplish the same feat. Keep in mind however, there might be more reasons why you could trigger no_results than simply having a Closed entry. As usual, test and figure out what works best for your project!

Here's what other people had to say

Picture of giovanni
giovanni
May 7, 2011 9:07 am

Cool tip!  But excuse my ignorance—- does one enter the ” status=“closed” ” tag for ALL one’s entries, so that if one gets clsoed i nthe future then it r-directs o 404?

i.e. it’s not done on a per entry basis after having closed an entry?

Picture of Ryan McLaughlin - nonactive
.(JavaScript must be enabled to view this email address) author
May 7, 2011 11:17 am

The code above is only placed in your single-entry template, separate from all other {exp:channel:entries} tags. Assuming you don’t have any closed entries at all, this bit of code gets ignored.

However, if in the future you ever decide to close one of your entries, it will cause any links out there on the web which point to that entry to now go to a 404 page.

It’s essentially insurance in case you ever have to close an entry… does that help?

Picture of giovanni
giovanni
May 7, 2011 5:08 pm

Yes.  It’s clear to me now that this is a good insurance for WHEN and IF one closes an entry,

Thanks again for the cool tip and your help.

Picture of Andy
Andy
May 10, 2011 12:46 am

Great tip - am just updating my base EE install atm, so will be implementing this (and a couple tips from the Planning ahead with your Expression Engine templates post too).
thanks smile

Picture of Robson Sobral
Robson Sobral
May 10, 2011 12:18 pm

Hi, I’m new in eecms.
Since channel:entries doesn’t return close entries by default, isn’t simpler to test if there’s no_results?

Picture of Ryan McLaughlin - nonactive
.(JavaScript must be enabled to view this email address) author
May 10, 2011 1:17 pm

Good point Robson. That would return the same result, assuming you aren’t doing anything fancy with the “Closed” status, or if you aren’t okay with having no_results in your page (an empty sidebar module or recent posts list might be acceptable, for example).

I’ll go ahead and update the article!

Picture of Andy
Andy
May 11, 2011 12:37 am

I tried a variation of this showing just the Title of the closed entry and a message saying that it had been closed with a couple helpful links for the visitor to find other interesting entries… I’m not sure a 404 is technically the correct message to give, as it’s not a broken page so much as a page where the entry is no longer public, but it did once exist.

Picture of giovanni
giovanni
May 11, 2011 9:08 am

@andy: nice!  i think i could use something like that. Would you be able to share the code?

Picture of Andy Marshall
Andy Marshall
May 11, 2011 9:25 am

From memory it was something like

{exp:channel:entries channel="my_channel" limit="1" status="closed"}
     {if status 
== "closed"}<h3>{title} has been closed. </h3>
<
p>Please check <a href="/{segment_2}">{segment_2}</a> for more entries</p>{/if}
{
/exp:channel:entries} 
Picture of Andy Marshall
Andy Marshall
May 11, 2011 9:26 am

Apologies for the slightly messy code formatting, but you get the idea smile

Picture of Ryan McLaughlin - nonactive
.(JavaScript must be enabled to view this email address) author
May 11, 2011 9:28 am

@Andy, I’m looking through the w3 status codes and I think 404 is still the best option. We associate 404 with a “broken” link, but really all it means officially is the page isn’t found, and there’s no indication whether that’s permanent or not (as opposed to the 410 status).

I think it’s acceptable to have a page return a 404 for a period of time, and then start working again in the future (I can’t seem to find a status that works better anyway)... correct me if I’m wrong.

I do agree, however, that using the URL to guess where the visitor might be headed and helping out with relevant links is always a good idea.

Picture of GDmac
GDmac
June 13, 2011 3:19 am

It is an extra entries:call i would rather skip. In most cases i already use a dedicated template for single entries with require_entry=“yes”. This makes it fairly straightforward that if no_results, we can redirect to 404, like Robson mentioned.

Picture of Rosalinda October 14, 2011 7:22 pm

HHIS I should have touhhgt of that!

What about you?

Name*

Email*

Website

Website

Please enter the word you see in the image below:


Comment*

Remember my personal information
Notify me of follow-up comments?