简体   繁体   English

如何修复 Laravel 5.1 - 404 Not Found?

[英]How can I fix Laravel 5.1 - 404 Not Found?

I am trying to use Laravel 5.1 for the first time.我第一次尝试使用 Laravel 5.1。 I was able to install it and https://sub.example.com/laravel/public/ is displaying what is should.我能够安装它并且https://sub.example.com/laravel/public/正在显示应该安装的内容。 However, views I create are giving me a 404 error Page not found.但是,我创建的视图给了我一个 404 错误页面未找到。

Here is what I have done so far:这是我到目前为止所做的:

I created a controller in laravel\\app\\Http\\controllers\\Authors.php我在laravel\\app\\Http\\controllers\\Authors.php创建了一个控制器

Here is the code behind the Authors.php file这是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());
    }
}

Then I created a view in laravel\\resources\\views\\Authors\\index.blade.php然后我在laravel\\resources\\views\\Authors\\index.blade.php创建了一个视图

Here is the code behind the index.blade.php file这是index.blade.php文件背后的代码

@layout('layouts.default')


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

Then I created a layout in laravel\\resources\\views\\layouts\\default.blade.php然后我在laravel\\resources\\views\\layouts\\default.blade.php创建了一个布局

Here is the code behind the default.blade.php file这是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>

Finally I created a route in laravel\\app\\Http\\routes.php最后我在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.但由于某种原因,我不断收到 404 错误页面未找到。

I enabled the mod_rewrite on my Apache 2.4.9 by uncommenting out the line我通过取消注释该行在我的 Apache 2.4.9 上启用了 mod_rewrite

LoadModule rewrite_module modules/mod_rewrite.so

Then restarted Apache.然后重新启动Apache。

From what I can tell in the php_info() output the mod_rewrite is enabled从我在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 

My current .htaccess file looks like this "which is the factory default"我当前的 .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]

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但是,当我去的时候,我仍然得到 404 页面

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

What am I doing wrong?我究竟做错了什么? How can I fix this problem?我该如何解决这个问题?

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.我看到了能够访问 / 路由的相同行为,但是当我第一次设置 Laravel 站点时,所有其他页面都返回 404。

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.在您的 apache 配置文件 httpd.conf 或 httpd-vhosts.conf 中,您需要启用可以放置在 .htaccess 文件中的指令。

Here is an example of my VirtualHost configuration:这是我的 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>

The key entry for your issue is AllowOverride All .您的问题的关键条目是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.这应该足以使网站正常工作,但您还可以包括optionsRequire all granted如果它们在整个网站上保持一致。

if you are on ubuntu you shoud do 3 thing.如果你在 ubuntu 上,你应该做 3 件事。 1. check if "/var/www/html/YourProject/public/ .htacess " is like this. 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. Add this lines on /etc/apache2/sites-available/000-default.conf 2. 在 /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 LoadModule rewrite_module modules/mod_rewrite.so

or或者

sudo a2enmod rewrite

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.如果您的 Laravel 路由不起作用,并且在您尝试加载页面时收到“未找到”错误,则您的 .htaccess 文件可能缺乏履行职责的权限。

The key is the apache AllowOverride directive.关键是 apache AllowOverride 指令。

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.如果您没有将 AllowOverride 设置为 All,您的 Laravel .htaccess 文件( /public/.htaccess )将无法启用 mod_rewrite,并且您的路由将无法工作。

The first step is to open your apache httpd.conf file.第一步是打开您的 apache httpd.conf 文件。 In OS X, it is located at:在 OS X 中,它位于:

/private/etc/apache2/httpd.conf

Option 1) Modify your main directive选项 1) 修改您的主要指令

This will change the AllowOverride value for all your websites.这将更改您所有网站的 AllowOverride 值。 In your httpd.conf file, look for the main directive:在您的 httpd.conf 文件中,查找主要指令:

<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.选项 2) 向您站点的指令添加指令。

<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.将更改保存到您的httpd.conf文件,重新启动或重新加载 apache,您的路由应该可以正常运行了。

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如果您在 Linux 上运行 Apache HTTPD 2.2.15(在我的例子中是 CentOS 6.7),那么目录指令将如下所示

<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.不过,除非您使用这些选项,否则您可能不需要 Options 行。

Thank you to the 2.4 answer.谢谢2.4的回答。 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.它帮助我在 2.2 上为我解决了这个问题,我还有另一台运行 2.4 的服务器,我也可以将它应用到。

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]

also, the URL is case sensitive to the folder name此外, URL对文件夹名称区分大小写

folder name: camelCase then the url must be localhost/camelCase/route-1文件夹名称: camelCase那么 url 必须是localhost/camelCase/route-1

hope this help someone xD, little bit silly希望这能帮助某人 xD,有点傻

首先,您应该通过按键盘上的Ctrl + s保存项目,然后再次测试。问题将得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM