Custom 404 - status code and error.php - help please

Need help with the Administration of your Joomla! 2.5 site? This is the spot for you.

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.
Locked
RobiCog
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Mon Aug 09, 2010 3:23 pm

Custom 404 - status code and error.php - help please

Post by RobiCog » Sun Jan 15, 2012 9:10 pm

(Joomla 1.7.3)

Hello,

I haven't been able to find a conclusive answer for this one in previous forum posts. Please help.

Google keeps showing old and non existent links as errors on my site. Recently learned that my custom 404 error page was not raising the required http status code 404 and hence causing long lists of 'not found' errors in google webmaster tools.

I have followed the instructions in the joomla documentation and have my error page set up in correct place.
Seems to be some debate about correct code to insert in the error.php file.

Official documentation says this code is correct (code sample - a)

defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );
if (($this->error->getCode()) == '404') {
header(‘Location: /index.php?option=com_content&view=article&id=157′);
exit;
}
?>

It doesn't work for me.

I found this code does (code sample - b)

defined( '_JEXEC' ) or die( 'Restricted index access' );
if (($this->error->getCode()) == '404') {
header('Location: ' . $this->baseurl . 'index.php?option=com_content&view=article&id=157');
exit;
}

1. Is the code sample b above okay to use?

2. I see others claim both sets of code are wrong and will not deliver the required 404 status code. Could someone confirm or correct this?

Thanks in advance.

Robi

RobiCog
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Mon Aug 09, 2010 3:23 pm

Revised: Custom 404 - status code and error.php - help pleas

Post by RobiCog » Thu Jan 19, 2012 4:27 pm

Hi, my challenge continues and since no answers to previous post I thought I'd update it with current status and hope someone might be able to help from here.

Issue started when I realised that my custom error page through a Joomla install was not raising the correct Http status code.

I have tried combinations of many things but only the server default 404 page will raise the correct status code. I have tried creating a custom 404 page through cPanel interface - creating the 404.shtml file. Although this does display - it still does not raise the correct code.

My current status:
-disabled SEF plugin
-Disabled joomla article custom 404 page
-created 404 page through cPanel
-.htaccess has custom statement added - ErrorDocument 404 /404.shtml
- in joomla template folder I have an error.php file override (as instructed by joomla docs) Here I have stated the path for error doc as MYDOMAIN.com/404.shtml. If this doc is not set then my custom 404 page does not show.

All incorrect urls are redirecting to the 404/shtml file/page as required but it now seems they are raising a 302 code "moved temporarily" Now I am really confused.

Am using http://web-sniffer.net/ for checking status codes.

Is there something I can do in cPanel, or Joomla to force the 404 code?

Other considerations I have looked at;

Ref: http://docs.joomla.org/Tutorial:Create_ ... Error_Page
Modify error.php to Redirect 404 Errors to your 404 Article
Edit the error.php file as follows, adding the code below immediately under the 'restricted access' line:

if (($this->error->getCode()) == '404') {
header('Location: /ABSOLUTE-PATH-HERE');
exit;
}
This does not work for me as it comes up with syntax errors.

Ref: http://docs.joomla.org/Custom_error_pages
If you want Joomla! to return a status code for the error, you can do so by adding logic before the DOCTYPE line, as follows:
<?php
if ($this->error->code == '404') {
header("HTTP/1.0 404 Not Found");
} ?>
I have not been successful in implementing this instruction.


Any help/advice would be appreciated.

Rob

RobiCog
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Mon Aug 09, 2010 3:23 pm

SOLVED: Custom 404 - status code and error.php - help please

Post by RobiCog » Sat Jan 21, 2012 6:29 pm

Much research, testing and evidence of no response to this post thread leaves me certain this is an issue without (currently) a workable solution without a 3rd party extension.

Discussions with my host, including some testing, indicate that the problem most likely lies within Joomla somewhere.

If you stumble upon this issue and are suffering the same - save yourself the time and install Qlue.

http://extensions.joomla.org/extensions ... data/16481

Works a charm.
Last edited by imanickam on Sun Jan 22, 2012 3:46 am, edited 1 time in total.
Reason: Replaced the extension's URL with the URL of the extension in JED

User avatar
jdwires
Joomla! Intern
Joomla! Intern
Posts: 97
Joined: Wed Aug 12, 2009 2:22 am

Re: Custom 404 - status code and error.php - help please

Post by jdwires » Wed Aug 08, 2012 8:02 pm

Having a similar problem and thought that I would share a bit of info. The response headers are sent before any php code is executed by the server.

Code: Select all

<?php header('Location: 404ErrorPageURLgoesHere'); exit;?>
The above code will actually overwrite the previously sent "HTTP/1.1 404 Not Found" to a "HTTP/1.1 302 Temporarily Moved". From what most any web-code jockey (myself included) will tell you, this can be SEO suicide.

So, in my opinion that there is a bug in the joomla code that will not return the proper HTTP Response header for SEO and other purposes when using template error pages.

My two cents. I'd love to be corrected on this one or even better yet, told how to fix the problem using the built-in functionality of this great CMS!

Thoughts anyone?

-Joel
Joel Wires

misholini
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Mar 10, 2010 1:00 am

Re: Custom 404 - status code and error.php - help please

Post by misholini » Fri Nov 01, 2013 9:05 pm

To use your custom 404 error page and return proper 404 http status rather than 302 redirect follow this:
Joomla 1.5
In your templates error.php, directly below:defined( '_JEXEC' ) or die( 'Restricted access' );
ADD:
if (($this->error->code) == '404') {
header("HTTP/1.0 404 Not Found");
echo file_get_contents(JURI::root().'/Your-404-Page');
exit;
}

Joomla 2.5
In your templates error.php, directly below:defined( '_JEXEC' ) or die( 'Restricted access' );
ADD:
if (($this->error->getCode()) == '404') {
header("HTTP/1.0 404 Not Found");
echo file_get_contents(JURI::root().'/Your-404-Page');
exit;
}

LouFlo
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Thu Nov 14, 2013 2:50 pm

Re: Custom 404 - status code and error.php - help please

Post by LouFlo » Sun Nov 24, 2013 10:27 pm

What about multilingual sites ?

LouFlo
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Thu Nov 14, 2013 2:50 pm

Re: Custom 404 - status code and error.php - help please

Post by LouFlo » Tue Nov 26, 2013 5:16 pm

I found somewhere else aboard multilingual custom 404 error should be translated pages from a same blog view with a disabled menu for each language then this as error.php file :

Code: Select all

<?php

defined('_JEXEC') or die;
if (($this->error->getCode()) == '404') {
	header('Location: /index.php?option=com_content&view=category&layout=blog&id=your_ID_#_here');
	$this->debug = false;
	exit;
}
?>
But it's not working here. I could only see my default language then I have to switch to another.


Locked

Return to “Administration Joomla! 2.5”