如何修复Laravel 5.1 - 404未找到错误?

10

我第一次尝试使用 Laravel 5.1。我已经成功安装了它,https://sub.example.com/laravel/public/ 显示的是正确的内容。但是,我创建的视图却显示404错误页面未找到。

到目前为止,我已经完成了以下步骤:

laravel\app\Http\controllers\Authors.php 中创建了一个控制器。

下面是 Authors.php 文件中的代码:

<?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());
    }
}

然后我在 laravel\resources\views\Authors\index.blade.php 中创建了一个视图。

这是 index.blade.php 文件的代码。

@layout('layouts.default')


@section('content')
    Hello, this is a test
@endsection

然后我在 laravel\resources\views\layouts\default.blade.php 文件中创建了一个布局。

这是 default.blade.php 文件背后的代码。

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

最后我在laravel\app\Http\routes.php中创建了一个路由。

<?php
Route::get('/', function () {
    return view('welcome');
});

Route::get('authors', array('as'=>'authors', 'uses'=>'authors@index'));

但出于某些原因,我一直收到404错误页面未找到的错误。

我通过取消注释这行代码启用了我的Apache 2.4.9上的mod_rewrite:

LoadModule rewrite_module modules/mod_rewrite.so

然后重新启动了Apache。

根据我在php_info()输出中看到的,mod_rewrite 已启用。

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 

我的当前的.htaccess文件看起来像这样“这是出厂默认设置”

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]

根据文档,我尝试将其更改为以下代码:

Options +FollowSymLinks
RewriteEngine On

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

然而,当我访问时仍然会出现404页面

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

我做错了什么?我该如何解决这个问题?

6个回答

21

当我第一次设置Laravel网站时,我发现可以访问/路由,但所有其他页面都返回404错误。

在您的apache配置文件httpd.conf或httpd-vhosts.conf中,您需要启用可以放置在.htaccess文件中的指令。

以下是我的VirtualHost配置示例:

<VirtualHost *:80>
    ServerAdmin admin@gmail.com
    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>

您的问题的关键入口是 AllowOverride All。这应该足以让网站工作,但如果它们在整个网站上保持一致,您也可以包括 optionsRequire all granted

我该如何在Windows服务器上实现这个?或者这可能是我的问题,我遇到了相同的问题,主页URL可以正常工作,但其他URL都无法工作,即使路由已经定义。 - Shamseer Ahammed

9

如果您使用的是Ubuntu系统,需要完成以下三个步骤。 1. 检查 "/var/www/html/YourProject/public/ .htacess " 文件是否如下所示。

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

请在/etc/apache2/sites-available/000-default.conf文件中添加以下内容:

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

注意:记住这是一个系统文件,因此您应该使用此命令

sudo gedit /etc/apache2/sites-available/000-default.conf 
  1. 启用重写模块。

    LoadModule rewrite_module modules/mod_rewrite.so

或者

sudo a2enmod rewrite

3
如果您的Laravel路由不起作用,并且每次尝试加载页面时都会出现“未找到”错误,则可能是因为您的.htaccess文件缺少执行权限。
关键在于apache AllowOverride指令。
如果您没有将AllowOverride设置为All,则您的Laravel .htaccess文件(/public/.htaccess)将无法启用mod_rewrite,您的路由将无法正常工作。
第一步是打开您的apache httpd.conf文件。在OS X中,它位于:
/private/etc/apache2/httpd.conf

选项1)修改您的主要指令

这将更改所有网站的AllowOverride值。在httpd.conf文件中查找主要指令:

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

只需将其更改为以下内容:
<Directory "/var/www/html">
    ...
    AllowOverride All
    ...
</Directory>

选项2)向您网站的指令中添加一个指令。
<VirtualHost 127.0.0.1:80>
    DocumentRoot "/var/www/html/epigroove/public"
    ...
    <Directory "/var/www/html/epigroove/public">
        AllowOverride All
    </Directory>
</VirtualHost>

保存更改到您的httpd.conf文件,重新启动或重新加载Apache,您的路由应该可以正常运行。


1
如果它在一个活动服务器上,尝试这个方法,虽然对我有效。
<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]


1
如果你正在运行Linux上的Apache HTTPD 2.2.15(例如我的情况是CentOS 6.7),那么目录指令将如下所示。
<Directory />
    Options Indexes FollowSymLinks Includes ExecCGI  
    AllowOverride All  
    allow from all  
</Directory>  

除非您使用这些选项,否则可能不需要Options行。

感谢2.4答案。它帮助我解决了在2.2上的问题,并且我还有另一个运行2.4的服务器也可以应用它。


0

此外,URL 对文件夹名称的大小写敏感。

如果文件夹名称为:camelCase,那么 URL 必须为 localhost/camelCase/route-1

希望这能帮助到某些人 xD,有点儿傻傻的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接