Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix Laravel 5.1 - 404 Not Found?

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?

like image 464
Junior Avatar asked Jul 22 '15 23:07

Junior


1 Answers

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.

like image 86
BalmainRob Avatar answered Oct 08 '22 08:10

BalmainRob