ADBLOCK_MSG
Custom 404 error page in Joomla 2.5Published: Tuesday, 27 August 2013 23:57 Written by Ogri Hits: 39329
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
-
Tuesday, 21 July 2020 03:18
Joomla 3: Redirect to same page after successful login -
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
Articles Most Read
-
148974
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments -
83763
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments -
57202
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-2. Template, editor, and other extensions -
40161
Editing animated GIF-images in Photoshop CS3 -
39329
Custom 404 error page in Joomla 2.5
Login
Guest Column
Recent comments
-
Custom 404 error page in Joomla 2.5
-
Roseann
02.11.2020 08:17
Noot spamming but wanted to say this is hugely useful!
-
Roseann
02.11.2020 08:17
-
Joomla: Integrate reCAPTCHA v2 (NO CAPTCHA) into JComments
-
PG SLOT
13.01.2021 09:21
Hey! I could have sworn I've been to this site before but after browsing through some of the post ...
-
Nidia
12.12.2020 08:04
HELP. I can't write a comment without it hanging.
-
Launa
17.11.2020 15:13
I can agree with the accuracy of this as I work in a related sector. Interesting cheers for sharing.
-
JD sports vouchers
04.10.2020 03:12
Happy to pay for something like this, or donate witth paypal even
-
PG SLOT
13.01.2021 09:21
-
Joomla: how to add your own language constants or override existing ones
-
Vouchersort
29.10.2020 11:33
Is this a free template that you are using as I really love it. As a website desugner myself I hope that ...
-
Vouchersort
29.10.2020 11:33
-
Migrating from Joomla 1.5 to Joomla 2.5. Epilogue
-
Dewitt
11.02.2021 06:17
HELP. I can't write a comment without the page freezing.
-
Dewitt
11.02.2021 06:17
-
Migrating from Joomla 1.5 to Joomla 2.5. Part 2-1. Transferring jDownloads and jComments
-
Francine
14.12.2020 08:49
I cant stomach this person so sorry x
-
Vouchersort
30.10.2020 15:43
Once again, great resource for us newbs.
-
Francine
14.12.2020 08:49
-
Unified filtering of mod_jcomments_latest module's output
-
Фильмы 2022 онлайн
03.12.2021 21:37
This web site definitely has all of the info I wanted about this subject and didn't know who to ask.
-
Фильмы 2022 онлайн
03.12.2021 21:37
Comments