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

kabeda's avatar

htaccess and my own 404 page

Hi, Here is my htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]



    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

As you can see, it's standard.

I want to add

ErrorDocument 404 /error404.html

in order to avoid the laravel exception which uses php. How can I do that?

Thanks,

0 likes
2 replies
bobbybouwmann's avatar

Yes this would normally work if you would go to some url that doesn't exist at all!. However you're talking to a PHP application. It can only throw a 404 whenever PHP has run, you can't do it before that. Also the current rewrite rule always points to index.php which will handle the routing for you!

So you have two options here: You need to whitelist all your routes in the htaccess file as well so you can then decide if you want to go to PHP or if you want to go to your custom html page.

Or you do the custom 404 page handling part by Laravel. If you create the resources/views/errors/404.blade.php file you can put anything you want in there ;)

Documentation: https://laravel.com/docs/5.8/errors#http-exceptions

kabeda's avatar

Hi, Thanks for your answer. In fact, I don't want to use the laravel exceptions. Our web server receives a lot of wrong urls, so my idea is to catch the 404 before calling to index.php. This is because for every request, a phppfm worker is called and this uses lot of ressources (60% cpu).

Please or to participate in this conversation.