Upd: Русская версия статьи: Кастомная страница 404 в Joomla 2.5.


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:

default-page-404

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.

  1. Create new category 404-en. Set the category language as English respectively. Save.
  2. 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 as 404-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:
  3. article-options

  4. Click Save. Article ID is being generated - store it:

article-id

  1. 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:

content-language

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.

  1. 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:

copy-article

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.

  1. 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.
  2. 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.
  3. 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.

Add comment


© 2011-2014 ogri.me
Use and reprint of the site materials are allowed and welcome. Direct indexable link to the used article or, at worst, to the site itself would be a good contribution to the author for his hard work.
Locations of visitors to this page