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

caddy's avatar
Level 4

Catch php file not found exception

Scrolling through my nginx errors log today, I noticed few attempts to access /wp-login.php which I believe to be the wordpress login page The server simply returns File not found. but I wanted to abort(404) instead I tried to add this login in my Exception Handler like this

    public function render($request, Exception $exception)
    {
        if($exception instanceof \Illuminate\Contracts\Filesystem\FileNotFoundException) {
            abort(404);
        }
        return parent::render($request, $exception);
    }

But this seems to be catching file storage not found exception is there a way to catch file not found exceptions in the public directory?

0 likes
2 replies
Sergiu17's avatar

public function render($request, Exception $exception) {

if($e instanceof \Illuminate\Contracts\Filesystem\FileNotFoundException) {

May be here is the problem?! This is what you need I guess, $exception in if statement, not $e

public function render($request, Exception $exception)
{
    if($exception instanceof \Illuminate\Contracts\Filesystem\FileNotFoundException) {
1 like
caddy's avatar
caddy
OP
Best Answer
Level 4

I ended up making a page rule on cloudflare, I just hated seeing an error on my nginx log and some people scanning my Laravel app as if it's a WordPress app Page Rule

Please or to participate in this conversation.