How to run Laravel in a subdirectory of a domain

Updated: January 15, 2024 By: Guest Contributor Post a comment

Introduction

Running a Laravel application in a subdirectory allows you to host multiple applications on a single domain, each in its unique path. This setup can be essential for organizing projects, creating a microservice architecture, or even for simple convenience. This tutorial will guide you through the process of configuring a Laravel application to run in a subdirectory of a domain.

Prerequisites

  • A Laravel application ready to be deployed.
  • Access to a server with Apache or Nginx installed.
  • Domain name with the ability to configure subdirectories.

Step 1: Preparing Your Laravel Application

Before deploying your Laravel application to a subdirectory, you need to ensure that your application’s .env file is properly configured with the correct URL setup. Here is an example of how your APP_URL might look:

APP_URL=https://example.com/subdirectory

Next, optimize your application for production using the following commands:

php artisan config:cache
php artisan route:cache
php artisan view:cache

Step 2: Configuring Your Web Server

Different web servers require different configurations. Below you will find instructions for both Apache and Nginx.

Apache Configuration

To configure Apache, set up a .htaccess file in your Laravel application’s public directory. Add the following directives:

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

Don’t forget to replace subdirectory with the actual subdirectory name where you want to host your Laravel application.

Nginx Configuration

For Nginx, you need to edit your server block configuration:

location /subdirectory {
    alias /path/to/laravel/public;
    try_files $uri $uri/ @laravel;
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

location @laravel {
    rewrite /subdirectory/(.*)$ /subdirectory/index.php?/$1 last;
}

This configuration points requests for your subdirectory to the public directory of your Laravel application.

Step 3: Laravel Routing

In your Laravel routes file (web.php), you may prepend the subdirectory to your route groups to make management easier:

Route::group(['prefix' => 'subdirectory'], function() {
    // Your application routes
});

Remember that you might have to modify links, asset calls, and API endpoints to reference the subdirectory correctly.

Step 4: Asset Management

Use Laravel’s asset helper to generate URLs for your assets. You can modify the helper to automatically prepend the subdirectory.

{{ asset('css/app.css') }}

In the above example, Laravel understands the base URL and appends the asset’s path correctly.

Step 5: Testing your Configuration

It’s very important to test your configuration. This can prevent any runtime errors due to misconfiguration. Visit https://example.com/subdirectory to see your application running.

Troubleshooting

If your application isn’t loading expectedly, here are a few things to check:

  • The server configuration files for typographical errors.
  • Laravel’s .env file for the APP_URL value.
  • A permissions issue in the Laravel framework directories.

Advanced Considerations

If your Laravel application relies on session or cache functionalities, you might need to adjust your server’s configuration to allow for these services to work correctly in the subdirectory context. It’s recommended to utilize Laravel’s support for secure, robust session and cache drivers suitable for production environments.

Scaling and clustering may also require a more intricate setup. In these cases, ensure that your subdirectory-driven strategy allows for proper reverse proxy configurations, load balancing, and potentially horizontal scaling.

Conclusion

Running Laravel in a subdirectory can streamline your web hosting environment and help organize your domain’s resources. By following the steps in this tutorial and ensuring all configurations are correct, your Laravel application should function seamlessly within its designated path.