Skip to content

Instantly share code, notes, and snippets.

@alexandreelise
Last active May 13, 2023 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreelise/541a4e6d3dcf07e2ff4bd99d52a71790 to your computer and use it in GitHub Desktop.
Save alexandreelise/541a4e6d3dcf07e2ff4bd99d52a71790 to your computer and use it in GitHub Desktop.
Handle Joomla 404 errors gracefully in 5 lines of code: Fr: Redirection erreur 404 vers un article Joomla en 5 lignes de code / En: Redirect incoming 404 errors to custom Joomla article in 5 lines of code
<?php
/** Redirection vers article personnalisé */
if ($this->error->getCode() == 404) {
header(sprintf('Location: %s', \Joomla\CMS\Router\Route::_('index.php?option=com_content&view=article&id=64', false, \Joomla\CMS\Router\Route::TLS_FORCE, true)));
exit(0);
}
@alexandreelise
Copy link
Author

En: Code snippet to put in error.php in your favourite template. Normally it should work. You can adapt the code as you wish to suit your needs but as-is it should work.

Fr: Bout de code à mettre dans le fichier error.php dans votre template préférée. Normalement ça devrait marcher. Vous pouvez adapter le code comme vous le souhaiter pour répondre à votre cas d'utilisation,mais, encore une fois, normalement ça devrait fonctionner.

@brianteeman
Copy link

this does not make a valid 404 page

@alexandreelise
Copy link
Author

Hi @brianteeman. Why is that? You just need to change the article id to match the article you want as 404 page. A joomler friend tried it and it works for him. Now having higher convertion rate even in case of the dreaded 404 page. Can you please tell me your use case. I'm curious to know why it didn't work for you.

@alexandreelise
Copy link
Author

To elaborate on the instructions to make this work:
1 - Create a Joomla article in your admin or frontend that will be used as custom 404 page.
2 - In the admin area make use you get the id of the article eg: 64
3 - Create hidden menu link using this single article and name it for example : What do you want to do?
4 - The alias of this article would be what-do-you-want-to-do
5 - If using Cassiopeia, for example. Create a child template named custom 404 (cassiopeia_custom_404)
6 - Create an override of the error.php by copying the error.php file from cassiopeia parent template in custom 404 child template.And copy the code snippet above without the opening php tag, below the use statements approximately line 17 in cassiopeia_custom_404 error.php you just copied from cassiopeia parent template.
7 - Save the error.php file
8 - Affect the menu link you created to this child template
9 - Type a non-existing url on your Joomla website.
10 - It should redirect you to the What do you want to do? custom 404 Joomla article page.

That's it. A more complete version of the 5-line php snippet.

@brianteeman
Copy link

This is not a 404 page. A 404 page has a status code of 404

image

This gist results ina statuse code of 302 (redirect) followed by a 200 (ok)

@alexandreelise
Copy link
Author

@brianteeman I understand your point and your concern. I should rename the title of this gist to be more precise and correct: How to handle 404 errors gracefully and redirect it to a custom Joomla article in 5 lines of code. The goal is to maximise the end-user of your site to see what you want them to see eg: special offer, a mailing list form, etc... Rather then a dull error that non tech-savvy people will understand. That is the use case that brought be to make this script. It was for a joomler friend that ask me how to do this as easy for him as possible. 5 lines is the best I could do for now.

@brianteeman
Copy link

The solution you offer here also used to be on the joomla docs site until it was pointed out that it is not a 404 page

https://docs.joomla.org/Archived:Creating_a_Custom_404_Error/en

@alexandreelise
Copy link
Author

Indeed. Blast from the past. Again @brianteeman I know you are absolutely right and this is not a 404 page. I rephrased it to be more clear. I changed the title. But that what my joomler friend's client wanted. Strictly speaking it is not a 404 page just as Joomla Web Services does not abide strictly to Roy Fielding's dissertation about REST. But as "real life, real world" examples and use cases occur we have to use what works for us. My friend's client is happy. So, to me, that's all that counts. One last thing. No-one is forced to use this script. If it does not fit someone's preferences they can freely ignore it. This is not a fight. I understand your point and I know you are right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment