Black Friday 2026 50% off every plan Our biggest deal of the year on #1 Joomla products.
Ends in 11d 15h 22m 03s
Claim 50% off
Awaiting an answer Helix Framework

How do I get the custom 404 error page to return a 404 code instead of 301?

Asked by Allen 1 year ago Last activity 7 months 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,https://,www.certifiedinfosec.com,/zdfhh
200,https://,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.

11 replies

Toufiq Senior Staff

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)

Allen Asked this

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?

Toufiq Senior Staff

Please check this URL.

Allen Asked this

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

Toufiq Senior Staff

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

Allen Asked this

??? 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!

Allen Asked this

Bump.

Toufiq Senior Staff

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;
}
Allen Asked this

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.

Toufiq Senior Staff

Use this file in your site.

templates/shaper_helixultimate/error.php

error.php.zip

Allen Asked this

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.

Allen Asked this

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

Toufiq Senior Staff

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

Allen Asked this

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

Log in to reply.