404 Error - Question | JoomShaper
🎃 Halloween Sale is Live! Get 35% OFF on all plans. Sale ends soon! Get Offer

404 Error

T

Takis

SP Page Builder 3 months ago

Hi, I use the error.php with your implementation about the custom 404 error handling.

// 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 .= 'foutpagina-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=38';
}
if (($this->error->getCode()) == '404') {
    header('Location: ' . $redirect_url, true, 301);
    exit;
}

In the default language works fine and redirects to 'foutpagina-404'. In the other language also redirects to the same and does not respect the translation '/en/error-page-404'

Should I have a different redirection in the error.php file?

0
6 Answers
Atick Eashrak Shuvo
Atick Eashrak Shuvo
Accepted Answer
Support Agent 3 months ago #199042

Thank you for your message.

You're correct that the current implementation redirects all 404 errors to a fixed alias (foutpagina-404), which works for the default language but doesn't account for multilingual setups. To ensure proper redirection based on the active language, you’ll need to enhance your error.php logic to dynamically detect the language and redirect accordingly.

Below is an improved version of your code that handles multilingual 404 redirects by detecting the current language and mapping it to the appropriate alias:

$app    = Factory::getApplication();
$lang   = $app->getLanguage()->getTag(); // e.g., 'en-GB', 'nl-NL'
$config = Factory::getConfig();

$sef         = $config->get('sef');
$sef_rewrite = $config->get('sef_rewrite');
$sef_suffix  = $config->get('sef_suffix');

// Define language-specific aliases
$languageRedirects = [
    'en-GB' => 'error-page-404', //replace with the URL of the page that you have copied for this language
    'nl-NL' => 'foutpagina-404', //replace with the URL of the page that you have copied for this language
];

// Default to base URL
$redirect_url = Uri::base();

// Build redirect URL based on language
if (isset($languageRedirects[$lang])) {
    $alias = $languageRedirects[$lang];

    if (!$sef_rewrite) {
        $redirect_url .= 'index.php/';
    }

    $redirect_url .= $alias;

    if ($sef_suffix) {
        $redirect_url .= '.html';
    }
}

// Trigger redirect on 404
if ($this->error->getCode() == 404) {
    header('Location: ' . $redirect_url, true, 301);
    exit;
}

This approach ensures users are redirected to the correct 404 page based on their selected language. Just be sure to update the aliases (or page IDs) to match your actual menu items.

Best regards

0
T
Takis
Accepted Answer
3 months ago #199045

I receive for both languages the following:

Sorry, there was a problem we could not recover from. The server returned a "500 - Whoops, looks like something went wrong."

https://www.splashandbeyond.nl/test (default lang) https://www.splashandbeyond.nl/en/test

0
Atick Eashrak Shuvo
Atick Eashrak Shuvo
Accepted Answer
Support Agent 3 months ago #199047

Hello Takis,

Sorry for the inconvenience you're experiencing. Please check again now. This method only works if your site is properly configured as a multilingual site, following Joomla's recommended steps. Please refer to this documentation: www.joomshaper.com/blog/how-to-create-a-multilingual-site-in-joomla-4-using-helix-ultimate

Best regards

0
T
Takis
Accepted Answer
3 months ago #199049

I use helix-ultimate and SP Page Builder and the Falang component for the translation.

0
T
Takis
Accepted Answer
3 months ago #199050

I send you the login info!

0
Atick Eashrak Shuvo
Atick Eashrak Shuvo
Accepted Answer
Support Agent 3 months ago #199051

Please note that this method is only effective when using the Joomla core multilingual system. It is not compatible with Falang.

0