Black Friday 2026 50% off every plan Our biggest deal of the year on #1 Joomla products.
Ends in 12d 09h 39m 41s
Claim 50% off
Staff replied SP Page Builder

404 error

Asked by Takis 1 year ago Last activity 1 year 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?

6 replies

Atick Eashrak Shuvo Staff

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

Atick Eashrak Shuvo Staff

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: https://www.joomshaper.com/blog/how-to-create-a-multilingual-site-in-joomla-4-using-helix-ultimate

Best regards

Takis Asked this

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

Takis Asked this

I send you the login info!

Atick Eashrak Shuvo Staff

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

Log in to reply.