CodeBlog.ch


Concrete5 – Custom 404 page

Posted in Concrete5, PHP by Remo Laubacher on the January 18th, 2010

It hopefullly doesn’t happen very often but sometimes a user might enter an address which doesn’t exist. In most situations a simple page is displayed, telling him that the page doesn’t exist. Having some more detailled information can be very helpful, especially when you just relaunched your website. Google & Co need a while to reindex your page and users will probably see “page not found” more frequently than usually.

Concrete5 allows you to customize this page but it needs a few modifications. Some of them are probably a bit hard to find – which is why I wrote this tutorial. The standard Concrete5 “page not found” page looks like this:

pagenotfound

As you can see, the layout uses the default concrete5 dashboard theme. First, we’d like to use our own theme! Open “config/site_theme_paths.php”, there are already a few commented examples in the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php  
 
defined('C5_EXECUTE') or die(_("Access Denied."));
 
$v = View::getInstance();
$v->setThemeByPath('/page_not_found', "yourtheme");
 
/* 
	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', "yourtheme");
 // $v->setThemeByPath('/403', "yourtheme");
 // $v->setThemeByPath('/register', "yourtheme");
 // $v->setThemeByPath('/dashboard', "yourtheme");
 
*/

In line 5 and 6 you can see the two lines I’ve added. Make sure that you replace “yourtheme” with the name of your theme – identical to the directory name of your theme in the folder “themes”.

Make sure your theme has a file called view.php. It might look similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
$this->inc('elements/header.php');
?>
 
 	<div id="content">
	   <div id="body">
	   	<!-- begin -->
	   	<?php
	   	echo $innerContent;
	   	?>
	   	<!-- end -->
	   </div>
	</div>	
 
<?php
$this->inc('elements/footer.php');
?>

$innerContent is where all the information is saved.

Once you’ve saved site_theme_paths.php your error page uses your own theme. In my case like this:

pagenotfound-custom

Sitemap

It might make sense to display a sitemap when a users landed on the 404 page. This is also easy to do. Create a copy of /concrete/single_pages/page_not_found.php here /single_pages/page_not_found.php. This makes sure we don’t modify files within the Concrete5 core. Open the file and make it look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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/>
 
<?php
$bt = BlockType::getByHandle('autonav');
$bt->controller->orderBy = 'display_asc';                    
$bt->controller->displayPages = 'top';
$bt->controller->displaySubPages = 'all';     
$bt->controller->displaySubPageLevels = 'all';                    
$bt->render('view');
?>		
 
<br/><br/>
 
<a href="<?php echo DIR_REL?>/"><?php echo t('Back to Home')?></a>.

Lines 13 to 20 uses the autonav block to display a sitemap. That’s it! In my case the “page not found” page looks like this:
pagenotfound-sitemap

A bit useless in my case but probably more helpful if you have more pages than I do.

7 Responses to 'Concrete5 – Custom 404 page'

Subscribe to comments with RSS or TrackBack to 'Concrete5 – Custom 404 page'.

  1. Justin said,

    on January 18th, 2010 at 16:24:40

    Thank you Remo! Been struggling with redirects in page_not_found.php, but this is a much better solution.

  2. Brian said,

    on January 18th, 2010 at 19:47:40

    Thanks for these great tutorials – just getting started with Concrete5 and I am constantly referring back to your site!

  3. Fernandos said,

    on January 23rd, 2010 at 18:01:59

    Hey thanks Remo! :)
    That’s a good tutorial, wish it was there earlier before I’ve gone the stony way to my 404 page with c5.. but mine looks like this, probably an inspiration for others: http://www.fif-moebel.de/404

  4. Ron said,

    on February 11th, 2010 at 08:28:47

    How can i make the nav render in the header.php of a theme globally for all single pages?

    Thx for this tutorial!


  5. on February 13th, 2010 at 12:36:54

    Anyone have an English translation please?


  6. on February 13th, 2010 at 13:55:15

    It is translated?!
    English: http://www.codeblog.ch/2010/01/concrete5-custom-404-page/
    German: http://www.codeblog.ch/2010/01/concrete5-custom-404-page/


  7. on February 13th, 2010 at 14:30:59

    Thanks, I didn’t even notice the German/English translation flags at the top of the page earlier. Thanks a lot, great procedure!

Leave a Reply