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

stevegoddard14's avatar

Capture and Handle 404s where a .php file is specified

We are migrating an old Laravel 4.2 app to Laravel 7.

When we first built the app several years ago, we created a db table of old URLs and their new paths so as to avoid google giving a 404. So if a user typed in www.website.com/old_address they would be automatically redirected to www.website.com/new_address.

In L4.2. we had the following code in app\start\global.php

App::missing( function(Exception $exception) {
    $p = Request::path();
    $r = ThreeO1Redirect::checkRedirect( $p ); //Just does a simple where search.
    if ($r) {
        RedirectHistory::createNew($p);
        return Redirect::to( $r->new_address, 301);
    }
    FourOFour::logError();
    return Response::view("404", [], 404);
});

In L7 we have added the following to app\Exceptions\Handler.php

//If it's a route not found, see if there is a redirect in the redirects table.
if ($exception instanceof NotFoundHttpException) {
    $p = request()->path();
    $r = ThreeO1Redirect::checkRedirect($p); //Simple where search.
    if ($r) {
        return redirect($r->new_address, 301);
    }
}

This works fine if the old address is something like 'articles'.

However, if the old address includes a php file extension (i.e. 'about_us.php') it fails with an error:

No input file specified.

I think Laravel is just looking for the specified file in the public dir. But i want it to treat this the same as any other route.

Any ideas??

Thanks

0 likes
5 replies
Dunsti's avatar

This is a setting in the webservers config.

For example in nginx: error_page 404 /redirect_script_for_missing_files.php;

For example in Apache: ErrorDocument 404 /redirect_script_for_missing_files.php

this leads all 404 errors to the specified script, where you can handle the 404 behaviour

stevegoddard14's avatar

Thanks @dunsti. I'm using Homestead. Is this something that i can tweak in my yaml file??

Thanks

Dunsti's avatar

you can't configure this in the .yaml-file directly.

There are two possibilities:

1.) after you created your vagrant-box (normally with homestead up) you ssh into your box (with homestead ssh) navigate to /etc/nginx/sites-available/ and change the config file there. (That's for nginx - not sure, where the apache-files are exactly, but they should also be somewhere in /etc/apache or similar)

But this needs to be done everytime you destroy and recreate your box !!!

2.) you can change the provisioning-script in your homestead installation, so this is set up automatically when you create your box. For nginx you need to adjust the file homestead/scripts/site-types/laravel.sh and for apache the file is named homestead/scripts/site-types/apache.sh

stevegoddard14's avatar

Thanks @dunsti . Still can't seem to get this to work.

I've added the following line to my website's ngnix config file:

error_page 404 /home/vagrant/my_website/laravel/public/index.php;

But i'm still getting the exact same issue: any non-existent routes that contain a .php extension are not be handled by laravel.

I'm yet to load the new site onto our dev/live server - just running locally at the mo. The dev server runs CentOS and apache - so i'm hoping that the issue will simply not exist once we move it from vagrant.

Steve

Dunsti's avatar
The dev server runs CentOS and apache

why are you changing the nginx-config then? try changing the apache-config ;)

Please or to participate in this conversation.