Custom 404 error page in Joomla 2.5Published: Tuesday, 27 August 2013 23:57 Written by Ogri Hits: 15302
Subject in the headline of this article had been seemingly described and discussed in the web many times. Nevertheless, as it often happens, the information in each source is incomplete. The overall picture is composed of disparate information, and some particular glitches which I encountered are not covered at all - at least I have not found such coverage and came up with my own solutions. Therefore, the purpose of this article (and all others) - to bring together information from different online sources, to structure it, supplement with my own experience and pass into judgement of the Internet community.
The default Joomla page 404 is not inscribed into template and cluttered with a bunch of technical information which is unattractive to a visitor:
The only link to the home page may not even come to the front. The lost guest closes your site in frustration without actually visiting it. We should immediately correct this misunderstanding.
The goal is: to create an informative page (if the site is multilingual - for each language), retaining structure of the site, but to make the page sort of separate - remove navigation Previous - Next, hide breadcrumbs and social media buttons, but keep comments. Also remove all the information of author, creation date, number of views (especially the latter ;)). Invalid address typed by visitor should remain in the address bar.
So, let's go to the process. I'd only like to note that, being the author of bilingual website at the time of writing (and not excluding to add more languages in the future), I made the code of the error page, taking multilingualism into account. I will describe the process for only one language (in my example - English), for other possible languages it should be simply reinstantiated replacing suffix ru
with the appropriate one.
Stages and steps:
Stage 1: Create and connect your 404 pages for each site language.
- Create new category
404-en
. Set the category language as English respectively. Save. - Create a new article. You can set just
404
, as the name for each languages, they will differ in their identifiers. Specify a category for the current one as404-en
. Language - English again. Utilize your creative imagination to generate a good looking and informative content. In the Article Options - tab hide everything you can: - Click Save. Article ID is being generated - store it:
- Edit the Joomla file of 404. Its location is:
[site_root]\templates\system\error.php
. Delete the contents of the file and copy the following code instead:
<?php defined('_JEXEC') or die; $errpages_by_lang = array( array('language'=>'en-Gb', 'lang'=>'en', 'page'=>'448') array('language'=>'ru-Ru', 'lang'=>'ru', 'page'=>'80'), ); $cur_language = JFactory::getDocument()->language; $sef_is_on = &JFactory::getConfig()->sef == 1; for ($i=0, $n=sizeof($errpages_by_lang); $i<$n; $i++) { if (strtolower($errpages_by_lang[$i]['language']) == strtolower($cur_language) || $i == $n-1) { if ($sef_is_on) echo file_get_contents(JURI::root().$errpages_by_lang[$i]['lang'].'/'.$errpages_by_lang[$i]['page']); else echo file_get_contents(JURI::root().'index.php?option=com_content&view=article&id='.$errpages_by_lang[$i]['page'].'&lang='.$errpages_by_lang[$i]['lang']); break; } } ?>
The key 'page'
in the array of each language contains article ID. Replace there mine (488) with yours your stored in the previous step. Let's discuss in detail what values are assigned to the keys 'language'
and 'lang'
. Go to the menu Extensions -> Language Manager -> Content. There click on the appropriate language, thereby opening the language edit page. In typical form, it looks like this:
Fill an array using these values. Key 'language'
corresponds to the Language Tag parameter, and 'lang'
takes value of the URL Language Code. If necessary, edit the values of these two keys for each language according to your settings.
Save changes made to the file error.php
. If your site is monolingual - skip next step.
- Repeat the above steps for the rest of languages. At the same time, so as not to specify settings manually each time, it's convenient to use the option of copying articles:
As it is clear, the error file's editing now includes only adding (or changing) a new language array as an element of array of languages, and also editing keys. 80 is the ID of my Russian page 404, replace as before with yours. And finally edit language tags if needed.
So, the pages of error 404 for all the site languages are created and attached. To test their behavior, type in the address bar of your something like http://ogri.me/nonexistent page. Redirect to our custom error page is now working. So let's pass to the next stage.
Stage 2: Configuring the interface of 404-pages.
- Navigation on the page is already hidden as our article is the only one in its category. But the breadcrumbs here are absolutely unnecessary. How to hide them is described in detail in the article Joomla 2.5: Remove breadcrumbs from specific pages just in terms of the page 404.
- Everyone decides separately, is it worth to keep the buttons of social networks. May be you created such a beautiful error page, that an enthusiastic visitor will want to post a link to it as a sample. For my part, I prefer to remove them to not to keep a stray reader of my website here, wishing him or her to quickly move on to the main content. Because, as I noticed, I've been embedding social media bar manually, I've been selectively detaching it by hand as well. This technique is described in an article Social Media Buttons in Joomla 2.5. Selective placement. Those who use plugins from third-party developers have to find the appropriate option in their settings (if there is one) or to get into their code and make some customization.
- But as for the comments, it is useful, in my opinion, to hook them up. In the text of the page you can also offer the traveler who have missed the right page to express one's displeasure. In JComments - just add categories 404-en and 404-ru separately by language to the list in Settings -> General -> Categories -> Choose categories for JComments to work in. I do not know how to do it in alternative components of comments, I never used them and do not plan: JComments is definitely the best.
Now I would like to separately dwell on one annoying moment I encountered during testing, and the disclosure of which I've never come across in articles on this topic. If there is a single quote in the wrong URL of the page, the Fatal error of type "Unprocessed exception" is occurring. Perhaps the developers will fix it in the new versions of Joomla, but for now I had to resolve this issue manually since the message of a fatal error is quite informative and leads to the file (\libraries\joomla\environment\uri.php
) and line number (194), where the shoe pinches. Open the file for editing, find the following code fragment:
// Check for quotes in the URL to prevent injections through the Host header if ($theURI !== str_replace(array("'", '"', '<', '>'), '', $theURI)) { throw new InvalidArgumentException('Invalid URI detected.'); }
From this code it is clear that when the URL includes single or double quotes, as well as < or > characters, framing tags in HTML markup, then the above-mentioned exception is generated. Moreover, when I tried to insert ", < or >, I had received Error 403 - Access forbidden. I replaced the above piece of code with a single line:
$theURI = str_replace(array("'", '"', '<', '>'), '', $theURI);
That is, instead of testing for harmful characters and throwing exceptions I've just deleted them from the link address. Now, even if they are in the URL, our custom 404 page will open. At the same time, all the user entered in the browser address bar remains unchanged, and that is good, he does not even know about our manipulations.
So now the 404 error page we created and configured is looking well. A visitor pressed wrong key or referred by dead link will not feel astray, and your website has all the chances to lure him or her to its contents. The main thing that your contents should have been interesting and informative. Well, now you are the doctor.
Latest News
-
Saturday, 30 December 2017 16:11
Joomla: how to add your own language constants or override existing ones -
Thursday, 30 November 2017 23:27
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
Saturday, 25 June 2016 15:33
Unified filtering of mod_jcomments_latest module's output -
Thursday, 17 September 2015 16:23
Post an illustrated Joomla-site article on Facebook using OG-tags -
Friday, 27 June 2014 13:35
Joomla: Content filtering by articles, categories and components
Articles Most Read
-
83541
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments -
27757
Editing animated GIF-images in Photoshop CS3 -
22184
Social Media Buttons in Joomla 2.5 -
22176
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-2. Template, editor, and other extensions -
21127
Migrating from Joomla 1.5 to Joomla 2.5. Part 1. Transferring content
Login
Guest Column
Recent comments
-
Editing animated GIF-images in Photoshop CS3
-
Lita Maivia 10.03.2016 22:22
-
Ogri 21.02.2015 00:01
You are welcome. I'm glad to have positive feedback.
-
Ryoko 20.02.2015 23:20
Thank you very much... This could help me a lot! ^^
-
-
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments
-
CHANHOTT 28.11.2018 04:21
Thank You
-
-
Joomla: Merge two sites into one using component J2XML
-
Ogri 11.01.2015 14:23
Hi Jenny, Sorry for the late response. As a matter of fact, you do not have to configure your web ...
-
Jenny McWilliam 31.12.2014 01:26
Hi , I am very interested in your article as I am trying to use J2XML and there is virtually NO ...
-
-
Photoshop: Processing photos of paintings for online gallery
-
siva 12.12.2017 06:13
Great work done. It is really good to visit your website and learn informative. I like the way you ...
-
geoff edwards 03.10.2017 10:56
Very clear instructions! One of the best Photoshop videos I have found. Even photographing art ...
-
-
Post an illustrated Joomla-site article on Facebook using OG-tags
-
writing service 09.05.2017 06:17
This article is use simple, straightforward sentences with few modifying phrases and clauses.I enjoy ...
-
-
Unified filtering of mod_jcomments_latest module's output
-
123movies 05.09.2018 17:54
Please let me know if you're looking for a writer for your blog. You have some really great posts and ...
-
Read more...