如何修复Laravel 5.1 - 404 Not Found?

Jun*_*ior 9 php apache .htaccess mod-rewrite laravel

我试图第一次使用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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

@layout('layouts.default')


@section('content')
    Hello, this is a test
@endsection
Run Code Online (Sandbox Code Playgroud)

然后我创建了一个布局 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>
Run Code Online (Sandbox Code Playgroud)

最后我创建了一条路线 laravel\app\Http\routes.php

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

Route::get('authors', array('as'=>'authors', 'uses'=>'authors@index'));
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我一直没有找到404错误页面.

我通过取消注释该行来启用Apache 2.4.9上的mod_rewrite

LoadModule rewrite_module modules/mod_rewrite.so
Run Code Online (Sandbox Code Playgroud)

然后重启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 
Run Code Online (Sandbox Code Playgroud)

我当前的.htaccess文件看起来像这样"这是出厂默认值"

选项-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]
Run Code Online (Sandbox Code Playgroud)

我还尝试根据[文档1]将其更改为下面的代码

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Run Code Online (Sandbox Code Playgroud)

但是,当我去的时候,我仍然得到404页面

https://sub.example.com/laravel/public/authors
Run Code Online (Sandbox Code Playgroud)

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

Bal*_*Rob 16

我看到了能够访问/ route的相同行为,但是当我第一次设置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>
Run Code Online (Sandbox Code Playgroud)

您的问题的关键条目是AllowOverride All.这应该足以使网站正常运行,但您也可以包括options,Require all granted如果它们在整个网站上保持一致.


Yos*_*lar 6

如果您在ubuntu上,则应该做3件事。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}]
Run Code Online (Sandbox Code Playgroud)

2.在/etc/apache2/sites-available/000-default.conf中添加此行

<Directory "/var/www/html">
 AllowOverride all
 Require all granted
</Directory>
Run Code Online (Sandbox Code Playgroud)

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

sudo gedit /etc/apache2/sites-available/000-default.conf 
Run Code Online (Sandbox Code Playgroud)
  1. 最后启用重写模块。

    LoadModule rewrite_module模块/mod_rewrite.so

要么

sudo a2enmod rewrite
Run Code Online (Sandbox Code Playgroud)