Our first 404 tutorial was helpful to a lot of people. However, some users also wanted the status code of the page to be a 404.
This tutorial will show you how to create a custom 404 page with a 404 status code in Joomla. Let's get started...
Step #1. Create the article
Create a Joomla article as you normally would. There's only one difference this time:
- Go to the Publishing tab
- Set the Robots settings to "No index, no follow"
Step #2. Create the menu item
Next, in your hidden menu, create a menu item for the article. You can again set the "No index, no follow" in:
- the Metadata tab
- Robots
Once you're done, copy your menu item's alias before saving.
Step #3. Edit the error.php file
Use Joomla's template manager, your host's file manager, or SFTP to navigate to your template folder (i.e. /templates/your-template). Find the error.php within it.
If the error.php file isn't present, create it.
At the top of the file, make sure there's the following check:
<?php defined('_JEXEC') or die;
Below it add:
if ($this->error->getCode() == '404') {
header("HTTP/1.0 404 Not Found");
echo file_get_contents(JURI::root().'add-the-copied-alias-here');
exit;
}
Step #4. Test
Finally, test the code and make sure it's working. Go to a page that doesn't exist on your site, e.g.: www.yoursite.com/doesnt-exist
And make sure your 404 article appears.
Also, open up your browser's inspection tool or Firebug. Navigate to the Net tab of the inspection tool, then refresh the page. You should see the status code as 404. Congrats!