skip to Main Content

I am attempting to install my Laravel 10 project in a subfolder so that I can access it at example.com/laravel-project/ instead of example.com/laravel-project/public/. Here is the .htaccess configuration I tried in the /laravel-project/ directory:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/ [L]
</IfModule>

This configuration results in a Laravel-styled 404 error when accessing the subfolder URL. I’ve reviewed other questions and it seems that something has changed in Laravel 10 that prevents this setup from working as it did in previous versions. I’d like to avoid using php artisan serve. Additionally, I have the following set in my .env file, which should also be correct:

#...
APP_DIR=laravel-project
APP_URL=http://example.com/laravel-project
#...

How can I configure the .htaccess file or Laravel 10 setup to correctly render the homepage at the subfolder URL without having to go to public/ in the URL?

Edit: The site will be on shared hosting

2

Answers


  1. Chosen as BEST ANSWER

    I managed to solve the issue. The .htaccess file with RewriteRule ^(.*)$ public/ [L] was correct. Here's the additional configuration I made:

    I added a new environment variable for the asset URL:

    ASSET_URL=http://example.com/laravel-project
    APP_DIR=laravel-project
    

    I updated the RouteServiceProvider (app/Providers/RouteServiceProvider.php) to prefix the routes with the subdirectory name:

    public function boot(): void
    {
        $this->routes(function () {
            Route::middleware('api')
                ->prefix(env('APP_DIR').'/api')
                ->group(base_path('routes/api.php'));
    
            Route::middleware('web')
                ->prefix(env('APP_DIR'))
                ->group(base_path('routes/web.php'));
        });
    }
    

  2. I would do it at the Web Server level using Virtual Host Aliases as you seem to be using Apache.

    Untested:

    <VirtualHost *:80>
        DocumentRoot /var/www/default-site
        Alias /laravel-project /var/www/laravel-app/public
    
        <Directory /var/www/default-site>
            AllowOverride All
        </Directory>
        <Directory /var/www/laravel-app>
            AllowOverride All
        </Directory>
    </VirtualHost>
    

    That should enable you to navigate to example.com/laravel-project and see your public folder.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search