Concrete5 – Custom 404 page

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
<?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
<?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)) { ?>
 
 
	<?php  $a = new Area("Main"); $a->display($c); ?>
<?php  } ?>
 
 
 
<?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');
?>		
 
 
 
<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.




14 Comments

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

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

Thx for this tutorial!

Thanks for the tutorial. Is it possible to also change the default text?

Page Not Found
No page could be found at this address.

Back to Home.

There are different ways to do that. One takes a few steps, I’ll see if I can write an article about it someday.
The other, easier but a bit ugly way would be to replace $innerContent. You can either remove it completely or do something like str_replace(‘Page Not Found’,’Back to Home’, $content) But as I said, this is a bit ugly.. (The correct solution would be an override to the complete single page, but as I said, this probably needs a bit more explanation)

Perhaps you can provide some insight on the structure of concrete5’s MVC framework. It appears that custom 404 pages within a package (packages/package_name/themes/theme_name) continue to override your local theme customizations (themes/theme_name).

I know that this is the case for the typography.css. Should you want to create local edits to the typography.css you have to copy over the package theme’s default.php which then forces you to pull over all of the package theme files.

Overall the goal is to only pull files that you want to modify into your local theme.

Any thoughts on this. In having a custom 404 page installed in a package prevented us from following your tutorial and override the 404 page within the root single_pages directory.

I can’t imagine how this should happen. A theme part of a package isn’t used at all until you install & activate it. The path /packages/package/themes/theme doesn’t have much use if you’re theme is located in /themes/theme.

Leave a Reply

Your email address will not be published. Required fields are marked *