How do I Get The Custom 404 Error Page To Return A 404 Code Instead Of 301? - Question | JoomShaper
Black Friday sale is live with flat 50% OFF. Sale ends soon! Grab your deal now!

How do I Get The Custom 404 Error Page To Return A 404 Code Instead Of 301?

A

Allen

Helix Framework 1 month ago

Hi,

I followed the directions at https://www.joomshaper.com/blog/create-a-custom-joomla-404-page-with-sp-page-builder to make my custom error pages for 404 and 403 errors. It works beautifully. However, the code returned to the requesting browser (googlebot) is 301 (permanent redirect) instead of 404. This is confirmed at https://tools.websiteadvantage.com.au/404-Error-Handler-Checker. This is also confirmed by @Pavel in his comment below the guidance. I tried Pavel's correction advice from 5 years ago, but it does not work today.

Per the recommendation at https://www.joomshaper.com/blog/create-a-custom-joomla-404-page-with-sp-page-builder, the custom code recommended that we added to our PHP looks like this:

// custom 404 redirect
$config = JFactory::getConfig();
$sef = $config->get('sef');
$sef_rewrite = $config->get('sef_rewrite');
$sef_suffix = $config->get('sef_suffix');
$redirect_url = JURI::base();
if(!$sef_rewrite) {
    $redirect_url .= 'index.php/';
}
$redirect_url .= '404'; // menu alias
if($sef_suffix) {
    $redirect_url .= '.html';
}
// if sef is turned off
if(!$sef) {
    $redirect_url = 'index.php?option=com_sppagebuilder&view=page&id=107';
}
if (($this->error->getCode()) == '404') {
    header('Location: ' . $redirect_url, true, 301);
    exit;
}

When a bad URL is used The requestor receives a 301 fro the redirect, and then a 200 for the new page. For example:

Status Code,Scheme,Host,Path
301,,www.certifiedinfosec.com,/zdfhh
200,,www.certifiedinfosec.com,/404

When a web server encounters a resource that does not exist, and a custom 404 page is configured to be displayed, the custom 404 page should still return an HTTP status code of 404 (Not Found).

What do I need to add or change to make the page return a proper 404 code for links that are no longer valid?

Thanks.

0
18 Answers
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #207605

Hi there,

Thank you for reaching out. To check the issue from our end, please create a staging site and share its login credentials with us. Make sure you provide only the staging site details, not the live site access. This is important because if any issue occurs while we’re working, it will affect only the staging site, not the live one. We want to maintain full transparency between the user and our support team.

Best regards,

Toufiqur Rahman (Team Lead, Support)

0
A
Allen
Accepted Answer
1 month ago #207613

The code I am using to redirect 404 error codes to a custom SPPB page is not custom. It is code provided by Joomshaper itself at https://www.joomshaper.com/blog/create-a-custom-joomla-404-page-with-sp-page-builder for over 5 years. Why do you need me to create a staging site on our server to test and fix the code Joomshaper created?

The code created and provided by Joomshaper does not correctly redirect, and then revert a 404 code back to the requestor as it should. It never has. This is not a unique or new issue. The problem was already independently noted by @Pavel over 5 years ago as you will see in the comments at the bottom of your own page providing the custom 404 page guidance. The comment is still there for you to read to this day, and is conveniently at the top of the comments ;-). Just click the link above!

Why are you asking me to create a staging site on our server? If you need a staging site to experiment and correct your own code you provide to all of your Joomshaper customers at your site, I recommend you create a staging site at your end.

Once you get it fixed, I further recommend you revise and correct the guidance Joomshaper has been providing at https://www.joomshaper.com/blog/create-a-custom-joomla-404-page-with-sp-page-builder for the past 5 years so you will not continue to propogate a recommendation that unnecessarily hurts SEO for your customers.

Aren't these reasonable and logical requests?

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #207699

Please check this URL.

0
A
Allen
Accepted Answer
1 month ago #207749

Hi Toufiq,

I see that you have a staging site there. What do you want me to do with this regarding this formum post? What is the purpose of giving me access to a staging site? Please clarify.

Regards,

Allen

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #207751

Reason to share website URL with login info, produce the issue on this site and we will check the issue.

0
A
Allen
Accepted Answer
1 month ago #207758

??? What???

I already provided all of the information to reproduce in the original post, and I don't need access to a staging server for any reason. I even provided screen captures and links for you to be able to validate that the code Joomshaper is recommending for creating a custom error page is indeed redirecting error codes to a custom page (I set mine to the home page to re-capture any SEO juice), but the redirection results in responding to the request with a redirect 301 code without responding with a proper 404 code for the original bad link.

Please re-read my original post carefully and respond with any questions you may have. Seriously. Please read the comments at the bottom of your own page at https://www.joomshaper.com/blog/create-a-custom-joomla-404-page-with-sp-page-builder as I already advised in the original post. Please. Pretty please. Pretty please with cherries on top!

0
A
Allen
Accepted Answer
1 month ago #207902

Bump.

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #208071

Here is the updated code

function getPageContentSecure($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 

$output = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch);

if ($http_code == 200) {
return $output;
} else {
return '404 Not Found';
}
}

$config = JFactory::getConfig();
$sef = $config->get('sef');
$sef_rewrite = $config->get('sef_rewrite');
$sef_suffix = $config->get('sef_suffix');
$redirect_url = JURI::base();
if(!$sef_rewrite) {
    $redirect_url .= 'index.php/';
}
$redirect_url .= '404'; // menu alias
if($sef_suffix) {
    $redirect_url .= '.html';
}
// if sef is turned off
if(!$sef) {
    $redirect_url = 'index.php?option=com_sppagebuilder&view=page&id=17';
}
if (($this->error->getCode()) == '404') {
    header("HTTP/1.0 404 Not Found");
    echo getPageContentSecure($redirect_url);
    exit;
}
0
A
Allen
Accepted Answer
1 month ago #208158

Hello,

This code you provide does not redirect to a custom SPPB home page. It simply provides error text, "404 Not Found", which is wat you have configured as the 404 message in the code itself. Apache already did this for me by default, so this doesn;t really do anything new.

The article I referred you to was supposed to provide a redirect for a custom SPPB 404 page. As I explained, the redirect works, even if the HTTP result code sent back to the requstor is incorrect.

In your suggested code above, it appears you just copied the code that @pulsar agency provided eight months ago in the bottom comments at https://www.joomshaper.com/blog/create-a-custom-joomla-404-page-with-sp-page-builder . Yeah, I read that too, tried his code, and already learned it only returns the text 404 message in the code itself. Thanks for making the message in English since @pulsar agency was in French. 😒

@pulsar agency's solution you migrated to this ticket does not achieve the objective of the very page it's on. Please advise per my orginal request.

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #208187

Use this file in your site.

templates/shaper_helixultimate/error.php

error.php.zip

0
A
Allen
Accepted Answer
1 month ago #208251

That did not work. It rendered its own 404 page pulling content from my custom sppb 404 page, but rendered it poorly with no Helix site menu.

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #208260

Please check this site and let me know.

https://helix.toufiq.net/

0
A
Allen
Accepted Answer
1 month ago #208264

Yours works and returns the proper 404 to the requestor. When I used your recent error.php, mine didn't render properly.

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #208265

Just copy the current code from the helix.toufiq.net and check again.

0
A
Allen
Accepted Answer
1 month ago #208269

Sorry, I'm an idiot. How do I get to your error.php code without file access?

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #208271

New file

error.php.zip

0
A
Allen
Accepted Answer
1 month ago #208273

Beautiful! You should probably post the code to the custom 404 error page guidance page at https://www.joomshaper.com/blog/create-a-custom-joomla-404-page-with-sp-page-builder .

0
Toufiq
Toufiq
Accepted Answer
Senior Staff 1 month ago #208276

I will do. Thanks

0