Apache2.4 & Laravel 5.4 with alias in a subfolder doesn't work without /index.php

Viewed 271

I've created an alias on apache server conf file (/etc/apache2/mods-enabled/alias.conf) to target the public folder.

    Alias /laravel/ "/home/user/dev/web/laravel/public/"

    <Directory "/home/user/dev/web/laravel/public/">
            Options Indexes FollowSymLinks MultiViews ExecCGI
            AllowOverride All
            Require all granted
    </Directory>

Also, I wrote the following .htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteRule ^(.*)/$ /$1 [L,R=301]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/laravel/(.*) /laravel/index.php/$1 [L]

</IfModule>

Now, when I'm visitng the http://IP/laravel the index.php is loaded as expected. But if I'm gonna make a call to a GET Route like http://IP/laravel/load_examples I'm getting back a NOT FOUND error.

Instead, if I visit the http://IP/laravel/index.php/load_examples, route loaded as expected.

It seems to me that something's going wrong with the .htaccess and rewrite rules.

I tried everything I found on the SO and other places but couldn't find any solution so far.

Any idea?

1 Answers

In the alias before the the code block shown try adding:

DocumentRoot /home/user/dev/web/laravel/public/

And then try this for your .htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>
Related