Home > Blockchain >  Rewrite rules for several laravel apps in the same Virtual Host
Rewrite rules for several laravel apps in the same Virtual Host

Time:12-04

I have an Apache2 virtual host where I'll be hosting several Laravel applications.

The VH document root is /home/user/applications/portal.

The laravel applications are in:

/home/user/applications/portal/larapps/larapp1
/home/user/applications/portal/larapps/larapp2
...

I want the URLs to be like:

http://portal.com/larapp1/...
http://portal.com/larapp2/...

So this is my config:

<VirtualHost *:80>
    ServerName portal.com
    DocumentRoot /home/user/applications/portal

    Alias /larapp1 /home/user/applications/portal/larapps/larapp1/public
    <Directory /home/user/applications/portal/larapps/larapp1/public>
        RewriteEngine On
        RewriteBase /larapps/larapp1/public/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^.*$ index.php [L]
    </Directory>
</VirtualHost>

If the request is http://portal.com/larapp1 then the route that matches is as I expect: Route::get('/',...).

But, inexpectedly, if I add any URI in the request, it doesn't do what I want. For example, if the request is http://portal.com/larapp1/foo, it doesn't match Route::get('/foo',...) but Route::get('/larapp1/foo',...).

What should I do so that routes are matched without the prefix /larapp1?

CodePudding user response:

I found it! My bad, I did a mistake in the configuration: I used a filesystem path in RewriteBase, when I should have indicated a base URI. This is the right config:

<VirtualHost *:80>
    ServerName portal.com
    DocumentRoot /home/user/applications/portal

    Alias /larapp1 /home/user/applications/portal/larapps/larapp1/public
    <Directory /home/user/applications/portal/larapps/larapp1/public>
        RewriteEngine On
        RewriteBase /larapp1/
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^.*$ index.php [L]
    </Directory>
</VirtualHost>
  • Related