Laravel 5 Apache mod_rewrite not working

16,290

The .htaccess file won't load up because it is in the public directory, which is really meant to be your document root. As such, you should be accessing the app by going to localhost/page/public/subpage.

If you want to use localhost/page/subpage whilst keeping your directory structure intact, then you need to add a new .htaccess file to the page directory, and delete the .htaccess file from the public directory.

/page/.htaccess contents:

<IfModule mod_rewrite.c>

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

    RewriteEngine On
    RewriteBase /page/

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

    # Send requests to public directory...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ public/index.php [L]

</IfModule>

Unfortunately, due to the way in which Laravel obtains the request information, you will need to group your routes to the page directory in your route configuration:

Route::group(['prefix' => 'page'], function()
{
    Route::get('subpage', ['as' => 'subpage', 'uses' => 'GenericPageController@subpage']);
    // Your other routes ...
});

Essentially, and in my opinion, this is the easiest way to work around this. You could also move the contents of your public directory up one level and change the paths accordingly in the bootstrap file.

Share:
16,290
Jan Schuermann
Author by

Jan Schuermann

I am a german web developer living in the Philippines. Working primarily with PHP (Laravel) &amp; Javascript (NodeJS, Angular 1+2, jQuery).

Updated on June 04, 2022

Comments

  • Jan Schuermann
    Jan Schuermann over 1 year

    Yes there, have been similar questions, but the suggested solutions are not working for me. So I've set up Laravel, and the installation works fine. Well, as long as I'm staying on the base route

    localhost/page/
    

    as soon as I'm trying to hit one of my routes, let's say

    localhost/page/subpage
    

    and the route is configured to return a view

    Route::get('subpage', ['as' => 'subpage', 'uses' => 'GenericPageController@subpage']);
    

    The method subpage in the Controller:

    public function subpage()
                {
    
                    return view('base.subpage');
                }
    

    I simply get a 404 response from the server whenever i try hitting one of the routes. It doesnt matter what controller I'm using, or if its returning a view or a closure.

    • mod_rewrite is enabled
    • On the main directory AllowOverride is set to All

    my .htaccess in the page/public folder looks like the following:

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

    Anyone has an idea what the problem could be?