Your Joomla website doesn't render page 404 correctly
It is known that in order to keep a visitor on the site, you need to properly handle HTTP / 1.0 404 and other similar codes. On the Internet you can find a lot of entertaining examples of 404 pages, as well as manuals - what and how to make 404 error handled correctly by the site for both visitors and search engines.
I want to discuss with you the 404 problem for Joomla sites.
General recommendations for configuring Joomla to handle HTTP / 1.0 404
I will not repeat here all the reasons for those or other steps and settings, I will list briefly what can be found on the Internet .
- We create in our Joomla "beautiful page 404". There are several ways - when implementing your special logic and the way they are selected for the
visitor; - We rewrite the error.php file into our template, which is used on the site as the main frontend template, from the system system template;
- Next, we edit this file in order to follow the following logic - if we caught a 404 error, then first display the HTTP / 1.0 404 header, and then give out the page that we prepared earlier. Suppose the number (ID) of our “beautiful 404 page” is 1001. The error.php file in your template may look like this:
defined('_JEXEC') or die;
if (!isset($this->error))
{
$this->error = JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR'));
$this->debug = false;
}
// Get language and direction
$doc = JFactory::getDocument();
$app = JFactory::getApplication();
$this->language = $doc->language;
$this->direction = $doc->direction;
if($this->error->getCode()=='404') {
header("HTTP/1.0 404 Not Found");
header('Location: index.php?option=com_content&view=article&id=1001');
}
Now check. Enter the address of the site. Next is abracadabra after the / character. Works? It works, as you would expect.
What's the catch?
Open the page debugging in your favorite browser (my favorite browser is Fitefox with Firebug), the “Network” tab, and look at the headers that the browser communicates with the site.
We enter the site address - the HTTP / 1.0 header 200 OK
Now gibberish ... We expect HTTP / 1.0 400 Not Found - we look at the headers:
- First HTTP / 1.0 302 Found
- Then our beautiful page is given to the browser with the HTTP / 1.0 code 200 OK
Why is this bad?
“But, after all, does it work?” - you say. Yes it works. And how does the search engine look at it?
I had a moving site pages from one section (folder) of the site to another. But not all pages had to move. The pages of the old section of the site were indexed. Those that moved were issued with the HTTP / 1.0 code 301 Moved Permanently (classics of the genre) and the search engines correctly moved them to a new place. And those that were supposed to “sink into oblivion” just flashed in the index, although they were physically absent on the site, and when accessing them, a “beautiful 404 page” was displayed, but not the HTTP / 1.0 404 code (see above).
Way out of this situation
For pages with 404 error, I decided to give the HTTP / 1.0 404 Not Found header and do not redirect through the Location header, but read the stream of the “beautiful 404 page” and redirect it to the browser. Here is the implementation:
if($this->error->getCode()=='404') {
header("HTTP/1.0 404 Not Found");
$url=JURI::root()."index.php?option=com_content&view=article&id=1001";
$data = file_get_contents($url) or die("Cannot open URL");
echo $data;
}
Now, the visitor is given the right page with an error 404, and the search engine really sees the code 404 and considers the address entered as such - Not Found.