Loading...
June 19, 2010#

Making use of the 404 Error Page in eZ Publish

For all commercial sites I have worked on in eZ Publish, no site has had a useful error page, despite the fact that using template overrides makes it so straightforward. In this post I will demonstrate how easy it is, showing how a custom 404 page for your site can be made. Our final page will contain a search box and the most popular pages on the site. We will also look at how you can use the eZPublish log to track which requests were not found.

Creating the Override

The most straightforward way of working out which file you need to override is to enable debugging on your site and make sure ShowUsedTemplates is set to enabled in your site.ini file (the settings you need are in the example below).

site.ini.append.php

[code lang="php"]
[TemplateSettings]
Debug=enabled
ShowUsedTemplates=enabled
[/code]

We can now see a list of templates used on any page by scrolling to the bottom of the page. If we enter a URL of a page which does not exist on the site, you should get a list of files similar to the below which is displayed:

Error Page Templates Loaded

From the screenshot we can see the requested template is error/kernel/20.tpl which was found in the standard design directory (design/standard/templates/error/kernel/20.tpl). Since our primary site access will be searched for the file before the standard design directory, we just need to add a file in the correct location and the file we create will be picked up instead. My site access is called onequarterenglish and so my file will be located within the following path of my design directory: onequarterenglish/templates/error/kernel/20.tpl.

To test it works, let’s create the file and then add some static html to the page:

onequarterenglish/templates/error/kernel/20.tpl

[code lang="html"]

Page Not Found

Sorry but we couldn't find the page you are after. Please try entering the page below in the search box or check out our most popular pages and search terms below.

[/code]

Your result should display similar to the below:

Basic Custom Error Page

Please note I have added some extra classes around the HTML so that the page fits in with the default eZ Flow site styling.

Now that we have a custom error page in place, let’s add some useful features to it.

Adding the search

This is the most straightforward thing to add. Since all searches can potentially be different, I would suggest copying the HTML from your current site search and using this. For my site, I am using the Solr powered eZ Find and so my search box comes from this. If you have not heard of eZ Find, I would recommend having a look at what it can offer as it is a lot more advanced from the basic eZ Publish search (it is included in most eZ Publish releases by default but it will not be enabled).

Here is the my code with the search box added:

onequarterenglish/templates/error/kernel/20.tpl

[code lang="html"]

Page Not Found

Sorry but we couldn't find the page you are after. Please try entering the page below in the search box or check out our most popular pages and search terms below.




For more options try the Advanced search

[/code]

Getting your most popular content

eZ Publish has a built in function for generating this. The view_top_list fetch function will return our most popular content for us. It works based on the server logs. Before we use it (assuming you are not using it already), you will need to setup a cronjob so the most popular are up to date.

Setting up the cronjob

I’m setting up a new CronjobPart for our cronjob. I’m assuming we only want to run it once a day during a quiet time so I will create a CronjobPart as follows:

cronjob.ini.append.php

[code lang="php"]
[CronjobPart-Daily]
Scripts[]=updateviewcount.php
[/code]

Once you have set up the cronjob part, set up your Daily part to run at a time of your choosing (for more details about setting up cronjobs in eZ Publish, see the documentation).

Before the cronjob is run, we also need to make sure eZ knows where our log file is located. Create a copy of settings/logfile.ini and add it to your site access settings directory. Once you have done so, update the file with the location and name of you apache log file.

To make sure our content is indexed, lets run the cronjob manually so that we have details of our most popular content. Using the command line, move to the directory eZ Publish is residing in and then run the following script:

php runcronjobs.php Daily

If successful, you should receive a message telling you that the view count has been updated. Now, let’s fetch the results.

Fetching the most popular content

Although you can limit the content by type, let’s just pull out the top 10 with no other restrictions. The following fetch function should do this for us:

[code lang="html"]
{def $popular_nodes=fetch( 'content', 'view_top_list', hash( 'limit', 10 ) )}
[/code]

Analysing pages not found

If you have the debug on your site enabled, one of the first entries will tell us the page cannot be found:

Debug Messages on pages not found

On your production site, these can be found in the error log which is found within the directory /var/log (you will find a few of them in there). To analyse them, open up a command prompt and move into the log directory. You can then use the following command to return you all of the relevant lines in the error log:

[code lang="php"]
grep Error occured using URI error*
[/code]

The results should appear similar to the below:

Results of a quick search on a local build

If you want to send the output to a file instead then adjust the command as follows:

[code lang="php"]
grep Error occured using URI error* > pagesNotFound.log
[/code]

Fixing solutions for URLs not found

If there are a couple of pages which come up repeatedly in your log results, there are a couple of things we can do:

  • Internal links – If the link has come from your site you can make sure the link is updated to contain the correct path
  • External links – For external links we can make sure that the incorrect link redirects to the correct link using a URL alias. You can add these in the CMS by clicking on the item symbol next to the item name within the content structure to bring up the context menu associated with the item and going to advanced->manage URL aliases (see below). Your users will then be redirected to the correct page.

    Adding a URL alias to a page

Further Reference