How can I fix Laravel 5.1 - 404 Not Found?

78,179

Solution 1

I see the same behaviour of being able to visit the / route but all other pages return a 404 when I first setup Laravel sites.

In your apache config file httpd.conf or httpd-vhosts.conf you need to enable the directives that can be placed in the .htaccess file.

Here is an example of my VirtualHost configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "C:/www/laravel_authority-controller_app/public"
    ServerName authoritycontroller.www
    ErrorLog "logs/AuthorityController.www-error.log"
    CustomLog "logs/AuthorityController.www-access.log" common
    <Directory "C:/www/laravel_authority-controller_app/public">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks Includes ExecCGI

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
    </Directory>
</VirtualHost>

The key entry for your issue is AllowOverride All. This should be enough to get the website working but you can also include options and Require all granted if they are consistent across the entire website.

Solution 2

if you are on ubuntu you shoud do 3 thing. 1. check if "/var/www/html/YourProject/public/ .htacess " is like this.

<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]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

2. Add this lines on /etc/apache2/sites-available/000-default.conf

<Directory "/var/www/html">
 AllowOverride all
 Require all granted
</Directory>

Note: remember that is an system file so you should use this comamd

sudo gedit /etc/apache2/sites-available/000-default.conf 
  1. by last enable rewrite module.

    LoadModule rewrite_module modules/mod_rewrite.so

or

sudo a2enmod rewrite

Solution 3

If your Laravel routes aren't working, and you're getting "Not Found" errors whenever you try and load a page, your .htaccess file is likely lacking permission to do its duty.

The key is the apache AllowOverride directive.

If you don't have AllowOverride set to All, your Laravel .htaccess file (/public/.htaccess) won't be able to enable mod_rewrite, and your routes won't work.

The first step is to open your apache httpd.conf file. In OS X, it is located at:

/private/etc/apache2/httpd.conf

Option 1) Modify your main directive

This will change the AllowOverride value for all your websites. In your httpd.conf file, look for the main directive:

<Directory "/var/www/html">
    ...
    AllowOverride None
    ...
</Directory>

Simply change it to this:

<Directory "/var/www/html">
    ...
    AllowOverride All
    ...
</Directory>

Option 2) Add a directive to your site's directive.

<VirtualHost 127.0.0.1:80>
    DocumentRoot "/var/www/html/epigroove/public"
    ...
    <Directory "/var/www/html/epigroove/public">
        AllowOverride All
    </Directory>
</VirtualHost>

Save the changes to your httpd.conf file, restart or reload apache, and your routes should be up-and-running.

Solution 4

If you are running Apache HTTPD 2.2.15 on Linux (CentOS 6.7 in my case), then the directory directive will look like this

<Directory />
    Options Indexes FollowSymLinks Includes ExecCGI  
    AllowOverride All  
    allow from all  
</Directory>  

You probably don't need the Options line though, unless you're using those options.

Thank you to the 2.4 answer. It helped me solve this issue for me on 2.2 and I have another server running 2.4 I can apply it to too.

Solution 5

if its on a live server, try this, it worked for me tho.

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /


# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

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

Share:
78,179
Junior
Author by

Junior

Updated on July 10, 2020

Comments

  • Junior
    Junior almost 4 years

    I am trying to use Laravel 5.1 for the first time. I was able to install it and https://sub.example.com/laravel/public/ is displaying what is should. However, views I create are giving me a 404 error Page not found.

    Here is what I have done so far:

    I created a controller in laravel\app\Http\controllers\Authors.php

    Here is the code behind the Authors.php file

    <?php
    
    class Authors_Controller extends Base_Controller {
    
        public $restful = true;
    
        public function get_index() {
            return View::make('authors.index')
                ->with('title', 'Authors and Books')
                ->with('authors', Author::order_by('name')->get());
        }
    }
    

    Then I created a view in laravel\resources\views\Authors\index.blade.php

    Here is the code behind the index.blade.php file

    @layout('layouts.default')
    
    
    @section('content')
        Hello, this is a test
    @endsection
    

    Then I created a layout in laravel\resources\views\layouts\default.blade.php

    Here is the code behind the default.blade.php file

    <!DOCTYPE html>
    <html>
        <head>
            <title>{{ $title }}</title>
        </head>
        <body>
    
        @if(Session::has('message'))
            <p style="color: green;">{{ Session::get('message') }}</p>
        @endif
    
        @yield('content')
        Hello - My first test
        </body>
    </html>
    

    Finally I created a route in laravel\app\Http\routes.php

    <?php
    Route::get('/', function () {
        return view('welcome');
    });
    
    Route::get('authors', array('as'=>'authors', 'uses'=>'authors@index'));
    

    But for some reason I keep getting 404 error Page not found.

    I enabled the mod_rewrite on my Apache 2.4.9 by uncommenting out the line

    LoadModule rewrite_module modules/mod_rewrite.so
    

    Then restarted Apache.

    From what I can tell in the php_info() output the mod_rewrite is enabled

    Loaded Modules 
    core mod_win32 mpm_winnt http_core mod_so mod_php5 mod_access_compat mod_actions mod_alias mod_allowmethods mod_asis mod_auth_basic mod_authn_core mod_authn_file mod_authz_core mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_dir mod_env mod_include mod_isapi mod_log_config mod_mime mod_negotiation mod_rewrite mod_setenvif mod_socache_shmcb mod_ssl 
    

    My current .htaccess file looks like this "which is the factory default"

    Options -MultiViews

    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]
    

    I have also tried to change it to the code below as per the documentation:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    

    However, I am still getting the 404 page when I go to

    https://sub.example.com/laravel/public/authors
    

    What am I doing wrong? How can I fix this problem?