Search code examples
apache.htaccesslaravellaravel-routingmod-alias

How to setup Apache to run Laravel rooted from a subpath?


The DocumentRoot of example.com is in /var/www/html.

Laravel is installed in /var/www/example/public and I can access it using http://example.com/dashboard but problem arises when I try to access other routes like as http://example.com/dashboard/login and is throwing 404 Not Found error. And saying The requested URL /var/www/example/public/index.php was not found on this server. Can anyone please guide me?

The .htaccess file in the public directory is untouched.

Here is the 000-default.conf:

ServerName example.com
UseCanonicalName Off
<Directory /var/www/html/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /dashboard /var/www/example/public
    <Directory /var/www/example/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>
#dynamic subdomain provisioning
<VirtualHost *:80>
    DocumentRoot /var/www/example/public
    ServerName user.example.com
    ServerAlias *.example.com
    <Directory /var/www/example/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Solution

  • I could solve using the following in the 000-default.conf file:

    <Directory /var/www/example/public>
        DirectoryIndex index.php
        AcceptPathInfo on
        AllowOverride All
        Options None
        Order allow,deny
        Allow from all
    </Directory>
    

    And had to change the laravel .htaccess in the following way:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
        RewriteBase /dashboard
    
        # Redirect Trailing Slashes...
        RewriteRule ^(.*)/$ dashboard/$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>