Skip to content

Instantly share code, notes, and snippets.

@alexandreelise
Last active May 13, 2023 14:44
Show Gist options
  • 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

@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