Richard Holloway Blog

Editing 404 page in Concrete5 CMS

Posted in April 2012 by under concrete5

The default 404 page in Concrete5 CMS is pretty basic and has a couple of problems. To make the 404 page a little more appealing, you may want to make the following changes:

Apply the site wide theme to the 404 page

The first thing you will notice is that the theme used for the rest of your site is not applied to the 404 page.

In order to apply the theme to the 404 page you need to know the name of the theme you are using. You can find this out on the site dashboard under "Pages & Themes" > "Themes".

My site uses "boldy" theme by Mnkras

Edit the file config/site_theme_paths.php and uncomment the commented out section at the bottom of this file, then put the name of your theme in each call of setThemePath().

So for "boldy" theme, my config/site_theme_paths.php file looks like:

A
<?php

defined('C5_EXECUTE') or die(_("Access Denied."));

/*
        you can override system layouts here  - but we're not going to by default

        For example: if you would like to theme your login page with the
        Green Salad theme, you would uncomment the lines below and
        change the second argument of setThemeByPath to be the handle
        of the the Green Salad theme "greensalad"

*/

$v = View::getInstance();

$v->setThemeByPath('/login', "boldy");
$v->setThemeByPath('/page_forbidden', "boldy");
$v->setThemeByPath('/page_not_found', "boldy");
$v->setThemeByPath('/register', "boldy");

Add some useful information

Having a page that just gives a link to the index page of you site is pretty minimal. You can help your visitors to find what they are looking for by adding more useful content. Some ideas include a site map, or a Google site search box. You may want to write your own missing page handler that search for pages which are similar to the words in the url.

For this site I have added links to the main sections of my site and a link to the site search .

To edit the page, you will first need to copy concrete/single_pages/page_not_found.php to single_pages/page_not_found.php. You should never edit files in the concrete/ folder as changes will be lost when you upgrade Concrete5 CMS later.

The default file looks like:

<?php  defined('C5_EXECUTE') or die("Access Denied."); ?>

<h1 class="error">php echo t('Page Not Found')?><h1>

<?php echo t('No page could be found at this address.')?>

<?php  if (is_object($c)) { ?>
        <br/><br/>
        <?php  $a = new Area("Main"); $a->display($c); ?>
<?php  } ?>

<br/><br/>

<a href="<?php echo DIR_REL?>/"><?php echo t('Back to Home')?></a>
            

To make things simple, remove the hard coded content from single_pages/page_not_found.php so it now looks like:

<?php  defined('C5_EXECUTE') or die("Access Denied."); ?>

<?php  if (is_object($c)) { ?>
        <br/><br/>
        <?php  $a = new Area("Main"); $a->display($c); ?>
<?php  } ?>
            

you can now edit this page within the Concrete5 dashboard. Go to "Pages & Themes" > "Single Pages" > "Page not found", and then edit the page as you would any other page.

Make sure your 404 page sends a 404 status

As previously mentioned, the 404 page in Concrete5 CMS returns status "200 OK", which means that search engines will index these as pages.

To make the 404 page send a status "404 Not Found", edit single_pages/page_not_found.php and add:

<?php header("HTTP/1.0 404 Not Found") ?>
            

so that the file finally looks like:

<?php  defined('C5_EXECUTE') or die("Access Denied."); ?>

<?php header("HTTP/1.0 404 Not Found"); ?>

<?php  if (is_object($c)) { ?>
        <br/><br/>
        <?php  $a = new Area("Main"); $a->display($c); ?>
<?php  } ?>
            

Now you should have a 404 page that looks part of the site, contains useful information for visitors trying to find information and returns a correct 404 code.

You can find other tips when getting started with Concrete5 by reading my post "Ten things you should do when using Concrete5 CMS"