【问题标题】:How can I fix Laravel 5.1 - 404 Not Found?如何修复 Laravel 5.1 - 404 Not Found?
【发布时间】:2015-10-13 01:47:15
【问题描述】:

我是第一次尝试使用 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 文件看起来像这样“这是出厂默认设置”

选项 - 多视图

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]

我也尝试根据documentation将其更改为以下代码:

Options +FollowSymLinks
RewriteEngine On

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

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

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

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

【问题讨论】:

    标签: php apache .htaccess laravel mod-rewrite


    【解决方案1】:

    当我第一次设置 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 有效,即使定义了路由。
    【解决方案2】:

    如果你在 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}]
    

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

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

    注意:记住这是一个系统文件,所以你应该使用这个comamd

    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,您的路由应该可以正常运行了。

      【讨论】:

        【解决方案4】:

        如果您在 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 的服务器,我也可以将它应用到。

        【讨论】:

          【解决方案5】:

          如果它在实时服务器上,试试这个,它对我有用。

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

          【讨论】:

            【解决方案6】:

            另外,URL 对文件夹名称区分大小写

            文件夹名称:camelCase 那么url必须是localhost/camelCase/route-1

            希望这可以帮助某人xD,有点傻

            【讨论】:

              猜你喜欢
              • 2016-10-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-01-25
              • 2021-06-03
              • 1970-01-01
              相关资源
              最近更新 更多