Deploy a Laravel Application in a Subdirectory on Apache Without a Virtual Host

Deploying a Laravel application on an Apache server typically involves configuring a dedicated VirtualHost. However, in shared hosting environments or when you want to serve Laravel from a subdirectory like https://example.com/myapp, you can skip setting up a new virtual host entirely.

This guide walks you through deploying Laravel in a subdirectory using only Apache’s .htaccess configuration — perfect for lightweight hosting or multi-app deployments under the same domain.


✅ Use Case

You have an existing website at https://example.com, and you want to host your Laravel application at:

https://example.com/myapp

Your Laravel app is located in:

/var/www/html/myapp

🧱 Step 1: Structure Your Laravel Application

Let’s assume your Laravel app is installed using Composer or already exists.

Place it inside your Apache web root:

/var/www/html/myapp/

Make sure the public folder is available at:

/var/www/html/myapp/public

You want users to access:

https://example.com/myapp/

…and be served content from Laravel’s public directory.


⚙️ Step 2: Set the RewriteBase in .htaccess

Inside the public folder of your Laravel app, you’ll find a .htaccess file. Modify it like this:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Define the base directory for Laravel
    RewriteBase /myapp/

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Route all non-existent files/folders to index.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

🔁 Replace /myapp/ with the actual subdirectory name in your deployment.


🔒 Step 3: Allow .htaccess Overrides in Apache

Apache needs to be explicitly told to honor .htaccess files. Open your Apache config (e.g. /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf) and find the <Directory> block for your document root (/var/www/html):

<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Restart Apache to apply changes:

sudo systemctl restart apache2

✅ Ensure mod_rewrite is enabled:

sudo a2enmod rewrite

🔧 Step 4: Update Laravel’s APP_URL

Open your .env file in the Laravel root directory and set the APP_URL:

APP_URL=https://example.com/myapp

Then, clear Laravel’s config cache to apply the changes:

php artisan config:cache

🛠️ Step 5: Fix Laravel’s Asset URLs (Optional)

If Laravel is generating incorrect asset URLs, you can force the asset paths in your AppServiceProvider:

// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\URL;

public function boot()
{
    URL::forceRootUrl(config('app.url'));
}

Or, if you’re using mix(), ensure that your mix-manifest.json paths are correct and match your subdirectory.


📦 Bonus: Serving Multiple Laravel Apps

Using this method, you can serve multiple Laravel apps from different paths:

  • https://example.com/myapp
  • https://example.com/anotherapp

Simply place each Laravel app in its own directory under /var/www/html/, update their .htaccess files and .env APP_URL settings.


✅ Conclusion

You don’t need to create a separate Apache virtual host to deploy Laravel applications in subdirectories. With the help of .htaccess and Laravel’s configuration files, you can:

  • Host multiple apps under the same domain
  • Use shared hosting environments effectively
  • Deploy Laravel projects quickly without admin-level Apache changes

This approach is perfect for developers who want fast deployment with minimal server config overhead.

This article is inspired by real-world challenges we tackle in our projects. If you're looking for expert solutions or need a team to bring your idea to life,

Let's talk!

    Please fill your details, and we will contact you back

      Please fill your details, and we will contact you back