404 error page

Discuss Search Engine Optimization in relation to Joomla! 3.x. This forum will also have discussions on SEF/SEO Joomla! 3.x extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
flashpointmm
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 145
Joined: Wed Aug 25, 2010 11:10 pm
Location: Australia

404 error page

Postby flashpointmm » Tue Mar 21, 2017 1:42 am

Hi,

I'm trying to follow the instructions laid out in the Joomla documentation for creating a custom 404 page...
https://docs.joomla.org/Creating_a_Cust ... Error_Page

It stated the code supplied should be placed
immediately under the 'restricted access' line:
however I cannot find the restricted access line in my Joomla installation error.php file!!!

Can someone please advise where it should go, or if the documentation I'm following is now outdated? I'm using J3.6.5

<?php
/**
* @package Joomla.Site
* @subpackage Template.system
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

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;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this->language; ?>" lang="<?php echo $this->language; ?>" dir="<?php echo $this->direction; ?>">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?php echo $this->error->getCode(); ?> - <?php echo htmlspecialchars($this->error->getMessage(), ENT_QUOTES, 'UTF-8'); ?></title>
<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error.css" type="text/css" />
<?php if ($this->direction == 'rtl') : ?>
<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error_rtl.css" type="text/css" />
<?php endif; ?>
<?php if ($app->get('debug_lang', '0') == '1' || $app->get('debug', '0') == '1') : ?>
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/media/cms/css/debug.css" type="text/css" />
<?php endif; ?>
</head>
<body>
<div class="error">
<div id="outline">
<div id="errorboxoutline">
<div id="errorboxheader"><?php echo $this->error->getCode(); ?> - <?php echo htmlspecialchars($this->error->getMessage(), ENT_QUOTES, 'UTF-8'); ?></div>
<div id="errorboxbody">
<p><strong><?php echo JText::_('JERROR_LAYOUT_NOT_ABLE_TO_VISIT'); ?></strong></p>
<ol>
<li><?php echo JText::_('JERROR_LAYOUT_AN_OUT_OF_DATE_BOOKMARK_FAVOURITE'); ?></li>
<li><?php echo JText::_('JERROR_LAYOUT_SEARCH_ENGINE_OUT_OF_DATE_LISTING'); ?></li>
<li><?php echo JText::_('JERROR_LAYOUT_MIS_TYPED_ADDRESS'); ?></li>
<li><?php echo JText::_('JERROR_LAYOUT_YOU_HAVE_NO_ACCESS_TO_THIS_PAGE'); ?></li>
<li><?php echo JText::_('JERROR_LAYOUT_REQUESTED_RESOURCE_WAS_NOT_FOUND'); ?></li>
<li><?php echo JText::_('JERROR_LAYOUT_ERROR_HAS_OCCURRED_WHILE_PROCESSING_YOUR_REQUEST'); ?></li>
</ol>
<p><strong><?php echo JText::_('JERROR_LAYOUT_PLEASE_TRY_ONE_OF_THE_FOLLOWING_PAGES'); ?></strong></p>
<ul>
<li><a href="<?php echo $this->baseurl; ?>/index.php" title="<?php echo JText::_('JERROR_LAYOUT_GO_TO_THE_HOME_PAGE'); ?>"><?php echo JText::_('JERROR_LAYOUT_HOME_PAGE'); ?></a></li>
</ul>
<p><?php echo JText::_('JERROR_LAYOUT_PLEASE_CONTACT_THE_SYSTEM_ADMINISTRATOR'); ?></p>
<div id="techinfo">
<p><?php echo htmlspecialchars($this->error->getMessage(), ENT_QUOTES, 'UTF-8'); ?></p>
<?php if ($this->debug) : ?>
<div>
<?php echo $this->renderBacktrace(); ?>
<?php // Check if there are more Exceptions and render their data as well ?>
<?php if ($this->error->getPrevious()) : ?>
<?php $loop = true; ?>
<?php // Reference $this->_error here and in the loop as setError() assigns errors to this property and we need this for the backtrace to work correctly ?>
<?php // Make the first assignment to setError() outside the loop so the loop does not skip Exceptions ?>
<?php $this->setError($this->_error->getPrevious()); ?>
<?php while ($loop === true) : ?>
<p><strong><?php echo JText::_('JERROR_LAYOUT_PREVIOUS_ERROR'); ?></strong></p>
<p><?php echo htmlspecialchars($this->_error->getMessage(), ENT_QUOTES, 'UTF-8'); ?></p>
<?php echo $this->renderBacktrace(); ?>
<?php $loop = $this->setError($this->_error->getPrevious()); ?>
<?php endwhile; ?>
<?php // Reset the main error object to the base error ?>
<?php $this->setError($this->error); ?>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 33421
Joined: Sat Apr 05, 2008 9:58 pm

Re: 404 error page

Postby Webdongle » Tue Mar 21, 2017 1:55 am

'defined('_JEXEC') or die;' restricts access

User avatar
boban_dedovic
Joomla! Apprentice
Joomla! Apprentice
Posts: 44
Joined: Tue Mar 28, 2017 7:09 am
Contact:

Re: 404 error page

Postby boban_dedovic » Tue Mar 28, 2017 7:38 am

Webdongle wrote:'defined('_JEXEC') or die;' restricts access


@Webdongle is correct. Any code under that line will show and be a "custom 404 page" for you.

I would recommend changing items within the HTML > body section since the other items are pretty useful. You would be able to add your own text and CSS styles there too.

But remember that you are referencing a CSS sheet already:

Code: Select all

<link rel="stylesheet" href="<?php echo $this->baseurl; ?>/templates/system/css/error.css" type="text/css" />


So, I would add my style items there and modify what exists.

I hope this helps!

flashpointmm
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 145
Joined: Wed Aug 25, 2010 11:10 pm
Location: Australia

Re: 404 error page

Postby flashpointmm » Tue Mar 28, 2017 7:54 am

Thanks to you both. Much appreciated... and it works :)

User avatar
jbumwad
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Nov 14, 2008 12:46 am
Location: The Wild West, USA

Re: 404 error page

Postby jbumwad » Wed Mar 29, 2017 12:37 am

flashpointmm wrote:Thanks to you both. Much appreciated... and it works :)


This week, I applied the same solution which also worked, except the HTTP status code returned is 302 instead of 404. The SEO impact of error page status codes has been a hot debate, yet even with Google's recent decision not to penalize the 302 status code, there still seems to be a negative effect in the case of a 302 status code for an error page (according to MoZ, it's an irrelevant page).

Another method used to work on my site. It stopped working after we upgraded to PHP 5.6.30—not sure why:

Code: Select all

if ($this->error->getCode() == '404') {
     header("HTTP/1.0 404 Not Found");
     echo file_get_contents(JURI::root().'add-the-copied-alias-here');
 exit;
 }


Therefore, I'm still working on customizing an error page within Joomla that also returns a 404 status code. Ultimately, rather than one that redirects to a Joomla article, it will likely be a hard-coded error.php file similar to what @boban_dedovic described earlier. If something helpful hashes out in the next couple of days, I'll post here.
The follow-through, the execution, is a thousand times more important than a "great idea." In fact, if the execution is perfect, it sometimes barely matters what the idea is. —Felix Dennis

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 33421
Joined: Sat Apr 05, 2008 9:58 pm

Re: 404 error page

Postby Webdongle » Wed Mar 29, 2017 2:29 am

jbumwad wrote:...
Therefore, I'm still working on customizing an error page within Joomla that also returns a 404 status code. Ultimately, rather than one that redirects to a Joomla article, it will likely be a hard-coded error.php ....

Create a menu item to your custom Joomla 404 Article

In .htaccess
ErrorDocument 404 /error404.php (or your error page)

In your error404.php just below defined('_JEXEC') or die; add

Code: Select all

if (($this->error->getCode()) == '404') {
header('Location: /yourpage.php?option=com_content&view=article&id=75');
exit;
}
Replace the location information (index.php?option..) with the URL from the menu item you created.
https://docs.joomla.org/Creating_a_Cust ... Error_Page
That should deliver your Joomla 404 page with a 404 header code.

Or you could reinvent the wheel ?

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 33421
Joined: Sat Apr 05, 2008 9:58 pm

Re: 404 error page

Postby Webdongle » Wed Mar 29, 2017 2:34 pm

Yep it works go to any non existing page on my site and the status code returns 404

User avatar
jbumwad
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Nov 14, 2008 12:46 am
Location: The Wild West, USA

Re: 404 error page

Postby jbumwad » Thu Mar 30, 2017 6:08 am

Webdongle wrote:
jbumwad wrote:...
That should deliver your Joomla 404 page with a 404 header code.

Or you could reinvent the wheel ?
Not at all, no desire to reinvent the wheel at all! I set up the htaccess and the error files as you indicated, and the system error page is still displaying instead of my custom article page. I'm checking all the components that create a cache at the moment, but the issue might be deeper than caching.
The follow-through, the execution, is a thousand times more important than a "great idea." In fact, if the execution is perfect, it sometimes barely matters what the idea is. —Felix Dennis

User avatar
jbumwad
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Nov 14, 2008 12:46 am
Location: The Wild West, USA

Re: 404 error page

Postby jbumwad » Thu Mar 30, 2017 7:14 am

Looking again, the path in the htaccess file needed to be :
ErrorDocument 404 /templates/templatename/error.php

So, the custom 404 article displays, but the error code is still 302.
The follow-through, the execution, is a thousand times more important than a "great idea." In fact, if the execution is perfect, it sometimes barely matters what the idea is. —Felix Dennis

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 33421
Joined: Sat Apr 05, 2008 9:58 pm

Re: 404 error page

Postby Webdongle » Thu Mar 30, 2017 10:33 am

jbumwad wrote:... the path in the htaccess file needed to be :
ErrorDocument 404 /templates/templatename/error.php

So, the custom 404 article displays, but the error code is still 302.


And /templates/templatename/error.php contains

Code: Select all

<?php

defined('_JEXEC') or die;

if (($this->error->getCode()) == '404') {
header('Location: /my-joomla-article-for-404.html');
exit;
}
Where my-joomla-article-for-404.html is the link to the 404 Article that you created in your Joomla. The header code returns 404 !!!

Personally I use Protostar and that 404 page uses the JERROR_LAYOUT strings to display the message ( I find it looks better). The header code for that also returns 404 !!!
error404 01.JPG
error404 02.JPG
error404 03.JPG
You do not have the required permissions to view the files attached to this post.

User avatar
jbumwad
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Nov 14, 2008 12:46 am
Location: The Wild West, USA

Re: 404 error page

Postby jbumwad » Fri Mar 31, 2017 8:30 pm

Webdongle wrote:Personally I use Protostar...
@webdongle, that's interesting. So do you actually have a Location: header in your error Protostar error page?

I ask because I've been testing different solutions, and my experience has been that the proper 404 status is returned as long as the location header is NOT in the error.php file. Here's why from the header chapter in the php.net manual:

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.

Code: Select all

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>


The follow-through, the execution, is a thousand times more important than a "great idea." In fact, if the execution is perfect, it sometimes barely matters what the idea is. —Felix Dennis

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 33421
Joined: Sat Apr 05, 2008 9:58 pm

Re: 404 error page

Postby Webdongle » Fri Mar 31, 2017 9:43 pm

All I know is that the link I gave you works because I tested it and that on my site with Protostar's 404 it works.

sdme255
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Sat Apr 01, 2017 11:21 am

Re: 404 error page

Postby sdme255 » Sat Apr 01, 2017 11:25 am

This code is working. Thank you

ravijit_c
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sun Apr 09, 2017 4:07 am

Re: 404 error page

Postby ravijit_c » Tue Apr 11, 2017 8:59 am

This question is little of topic but, i want to know how can i make my 404 with search and popular article liks?

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 33421
Joined: Sat Apr 05, 2008 9:58 pm

Re: 404 error page

Postby Webdongle » Wed Apr 12, 2017 10:13 am


johnsimth774
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Thu Aug 03, 2017 6:41 am

Re: 404 error page

Postby johnsimth774 » Wed Aug 09, 2017 9:37 am

Thank you for sharing. This code is working.

User avatar
leolam
Joomla! Master
Joomla! Master
Posts: 18417
Joined: Mon Aug 29, 2005 10:17 am
Location: Netherlands/ UK/ S'pore/Jakarta/ North America
Contact:

Re: 404 error page

Postby leolam » Wed Aug 09, 2017 4:11 pm

For those who do not like coding or if it takes too much time just purchase (cheap) this extension https://extensions.joomla.org/extension ... -404-page/

Leo 8)
Celebrating 12-Years of Professional Joomla Support Services
- Joomla Professional Support:https://gws-desk.com -
- Joomla Specialized Hosting Solutions:https://gws-host.com -
- Member Joomla Bug Squad & J-CMS Release Team

mryo
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sun Jul 30, 2017 4:14 pm

Re: 404 error page

Postby mryo » Sun Aug 13, 2017 6:41 am

leolam wrote:For those who do not like coding or if it takes too much time just purchase (cheap) this extension https://extensions.joomla.org/extension ... -404-page/

Leo 8)


Thank you so much, that lightens up all the works!

EditaStewart
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Aug 18, 2017 1:13 pm

Re: 404 error page

Postby EditaStewart » Fri Aug 18, 2017 1:25 pm

can i change the code of the 404 erorr page?


Return to “Search Engine Optimization (Joomla! SEO) in Joomla! 3.x”

Who is online

Users browsing this forum: Hackwar and 4 guests