1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

How to redirect or show custom 404 for css and js folders in Laravel?

Discussão em 'Outras Linguagens' iniciado por Anatoly Lindemann, Outubro 17, 2024 às 16:23.

  1. How can I redirect users to the website’s root or show a custom 404 page when they attempt to directly access the css and js directories under the public folder by entering a URL in the address bar? For example, visiting these URLs: https://example.com/css and https://example.com/js.

    I'm using Laravel 10 with OpenServer 5 (Apache and PHP 8.1). I haven't changed any apache and php settings, everything is default from OpenServer.

    When I try to visit https://example.com/css in my project, I get the error shown in the screenshot. [​IMG]

    In Laravel, I've set up my routes so that when a user navigates to a non-existent route, they are redirected to the homepage (example.com/). This works as expected, but folders inside the public directory (such as css, js, fonts) ignore this behavior and instead return the error shown in the screenshot.

    What I want is that if someone tries to access a route like https://example.com/css, they should either be redirected to the homepage (example.com/) or see a nice custom 404 page instead of the error from the screenshot. Can this be done using Laravel?


    On real sites like dev.to or laravel.com, if you try to access directories like dev.to/assets or laravel.com/build, they show a clean 404 page, not the Apache error like in my case.

    Please be patient with me. I’m a complete beginner in development, and instead of making fun of me or acting condescending, please guide me on the right path.

    Continue reading...

Compartilhe esta Página