skip to Main Content

I deployed a laravel/inertia/vue project on apache in a subdirectory of /var/www/html – let’s call it “myapp”.

On the browser: http://localhost/myapp shows as http://localhost/myapp/myapp, repeating myapp.
The Virtual host settings on 000-default.conf are as follows:

… Original settings

#Settings for myapp
 Alias /myapp /var/www/html/myapp/public
 <Directory /var/www/html/myapp>
    AllowOverride All
    Options -Indexes
 </Directory>

The .htaccess on myapp/public is the original with the following line added:
RewriteBase /myapp/

Everything works fine in myapp except, for example when showing an item

http://localhost/myapp/myapp/users/3 and press refresh on the browser, I get 404 not found because the working is is actually http://localhost/myapp/users/3 without the repeated myapp.

Is there a way to setup the deployment so it does not repeat myapp and when I type
http://localhost/myapp it actually is http://localhost/myapp,

I have tried several suggestion by googling the problem stated but I have not been able to find a solution

2

Answers


  1. It seems like the configuration needs a small adjustment to resolve this behavior.

    To address the repeated subdirectory issue, please follow these steps:

    1. Update .htaccess in public Directory:
      Inside the public directory of your Laravel application (myapp/public), ensure that the .htaccess file contains the following line:
    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    

    This step is already mentioned in your description, but just make sure it’s correctly implemented.

    1. Clear Configuration Cache:

    After making these changes, clear your Laravel configuration cache using the following command:

    php artisan config:clear
    
    1. Restart Apache:

    Restart Apache to apply the changes to your virtual host configuration:

    sudo service apache2 restart
    

    Reference:
    https://lucabecchetti.medium.com/configure-laravel-to-work-in-a-subdirectory-and-with-artisan-serve-at-the-same-time-654ba0c1fd0b

    Login or Signup to reply.
  2. The inertia-laravel has a bug which makes a route (eg. http://localhost/my-project) change to (eg. http://localhost/my-project/my-project)

    If you are using inertia in your project and having repeated sub-directory issue, this could be the reason.

    The issue has been resolved in latest version. Issue link

    Update inertiajs/inertia-laravel to resolve.

    Or

    You want to fix manually, do the following steps.

    // Edit following file
    vendor/inertiajs/inertia-laravel/src/Response.php
    
    // Find the following line from the code
    'url' => $request->getBaseUrl().$request->getRequestUri(),
    
    // Replace the line with the following 
    'url' => $request->fullUrl(),
    
    // OR as per the latest update replace with: 
    'url' => Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/'),
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search