Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VictorMV's avatar

Setting a Multilanguage URL for a custom 404 page in a Laravel project

Hello!

I'm currently working on a project in Laravel. I've customized the SetLanguage.php middleware to be able to redirect user in case of a wrongly entered URL address in my website. However, the 404 page remains a huge problem. I have to find a way how to make it so that when my website changes its language from its language bar, the URL of the 404 page also changes to the current locale.

Here's my code so far (they are two classes:)

SetLanguage.php

Helpers\ErrorRedirectHelper.php

I'm open to any suggestions if anyone would be able to help. Here's some more info about my intent in case the logic is confusing: I'm intending to check if the user is entering wrong URL addresses in my website and then redirect them to my custom 404 page. The user would probably enter something like 'www.mywebsite.com/jfkjfejk', 'www.mywebsite.com/en/jfkjfejk', 'www.mywebsite.com/jfkjfejk/htththth' and during all of these similiar cases, the website should always redirect to my 404 page which is linked to an existing controller method. My task is to find how to change my code so that it can surely save the preferred user language even if the page's URL looks something like this 'www.mywebsite.com/a/jfkjfejk/0'. My project has its routes configured like this:


-- routes/web.php
   ...

        Route::group(['prefix' => '{language}'], function () {
        	Route::get('index', [App\Http\Controllers\HomeController::class, 'index']);
 		});

       Route::fallback(function () {
           return redirect('/{language}/ERRORS/404_-_NON-			 
              EXISTENT_CONTENT/The_requested_page_does_not_exist/121');
        });

0 likes
0 replies

Please or to participate in this conversation.