Search code examples
apache.htaccesslaravel

Laravel Routes not working, Apache configuration only allows for public/index.php/route


Example:

Route::get('/get', function() {
    return 'get';
});

To view the route above, I must navigate to public/index.php/get.

I've viewed quite a few SO posts and googled around trying different things and it hasn't made a difference (yes I restart apache every time).

Here is my .htaccess in the public directory:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
</IfModule>

# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

What could be causing this still? I'm running Ubuntu.


Solution

  • Do you have mod_rewrite installed and enabled on apache? Try to remove the if lines ( and ) and see if it throws an error when you try to load the website. If it does, run sudo a2enmod rewrite and restart apache.

    This is the .htaccess I have on my public/ directory:

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