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

CodeCanyon's avatar

Laravel 10 + Inertia + ReactJS getting 404 page on browser refresh

Hi, I just did setup a fresh Laravel 10 project with Inertia with ReactJS. This is the very first time I use Inertia and React with a Laravel project. And please note this is a fresh project, and I didn't do any modifications to the code.

Once I loaded the welcome page normally Laravel URL is with 'localhost/public' but with Inertia I noticed that URL is like 'ocalhost/public/public'. It's strnge for me.

And once I clicked on login link it goes to the page like 'http://localhost/public/public/login'. Then if I do a browser refresh manually, I get a 404 page.

Any idea why it happens?

0 likes
5 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue is likely caused by Laravel's default configuration for handling client-side routing. When the user refreshes the page, the server tries to find a matching route for the URL, but since the URL is now pointing to a client-side route, it returns a 404 error.

To fix this, you need to configure Laravel to always return the same view for any client-side route. This can be done by adding a catch-all route to your web.php file:

Route::get('/{any}', function () {
    return view('app');
})->where('any', '.*');

This route will match any URL and return the "app" view, which is where your Inertia app is mounted. This way, when the user refreshes the page, Laravel will always return the same view, and your client-side routing will take over from there.

Make sure to add this route after all your other routes, so that it only catches URLs that don't match any other route.

Also, make sure to remove the "public" from your URL. You can do this by configuring your web server to point to the "public" directory of your Laravel project, or by using Laravel Valet or Homestead.

Once you've made these changes, your URLs should look like this:

And refreshing the page should no longer result in a 404 error.

1 like
CodeCanyon's avatar

I tried AI given solution, but now I'm getting

ErrorException PHP 8.1.19 10.12.0 Undefined variable $page

From this line exactly.

@vite(['resources/js/app.jsx', "resources/js/Pages/{$page['component']}.jsx"])

dkent13's avatar

@CodeCanyon How did you resolve this? I added the line generated above, but continue to receive this error. Thanks

CodeCanyon's avatar

@dkent13 Did you configure the server to point to the "public" directory of your Laravel project?

CodeCanyon's avatar

Wait.. issue gone now :)

I need to add the given code snippet:

Route::get('/{any}', function () { return view('app'); })->where('any', '.*');

After the require DIR.'/auth.php'; line as well.

@laryai is awesome!!

Thanks buddy.

I think this is one of the best use cases of AI :)

Please or to participate in this conversation.