reactjsdeploymentcpanelinertiajslaravel-12

Inertia routes return 404 (Not Found) for app deployed in subfolder


I have deployed a Laravel 12 + Inertia.js (React) app on a shared hosting (cPanel). The app is located in a subfolder: https://mydomaine/app/.

Authentication routes like:

work fine.

But all custom Inertia routes return a 404 Not Found

What I've tried:

In my .env I set:

APP_URL=https://mydomain/app

My .htaccess in /app/public:

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

    RewriteEngine On
    RewriteBase /app/

    # 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]

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

I also patched app.tsx to prefix all Inertia visits and <Link> with /app.

But the error is still there.


Solution

  • I found a solution i just add a condition in app.tsx

    const basePath = '/app'; // sub floder
    
    // Intercept all Inertia requests to enforce the correct prefix
    router.on('start', ({ detail: { visit } }) => {
        const path = visit.url.pathname;
    
    //add prefix if needed
       if (path.startsWith('/') && !path.startsWith(basePath)) {
            visit.url.pathname = basePath + path;
        }
    });