Laravel 除“/”外的所有路由都返回 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24147460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:37:44  来源:igfitidea点击:

Laravel all routes except '/' return 404

php.htaccesslaravellaravel-4

提问by hansn

As the title says, all the routes in my laravel app except the home('/') route result in a 404 error.

正如标题所说,我的 Laravel 应用程序中除 home('/') 路由之外的所有路由都会导致 404 错误。

I have separated the public folder from the rest of the laravel app as represented in the folder structure below. EDIT: I have confirmed that this is not what's causing the problem.

我已将公共文件夹与 Laravel 应用程序的其余部分分开,如下面的文件夹结构所示。编辑:我已经确认这不是导致问题的原因。

This error occurs on both the development system (local) and the production system (shared hosting).

此错误发生在开发系统(本地)和生产系统(共享主机)上。

EDIT: I forgot to mention: routes work if I go to localhost/index.php/route_name

编辑:我忘了提到:如果我去 localhost/index.php/route_name,路由就可以工作

Folder structure: (folder names changed to public/ and laravel/ for convenience)

文件夹结构:(为方便起见,文件夹名称改为public/和laravel/)

.
+-- public/
|    +-- index.php
|    +-- packages/
|    +-- etc...
+-- laravel/
|    +-- app/
|    +-- artisan
|    +-- etc...

routes.php:

路线.php:

<?php
Route::get('/', function() // Only this route works
{
    return 'hello world';
});

Route::get('oversikt', function() // This route does not work
{
    return 'goodbye world';
});

bootstrap/paths.php:

引导程序/paths.php:

<?php
return array(
    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../../pc',
    'base' => __DIR__.'/..',
    'storage' => __DIR__.'/../app/storage',
);

index.php:

索引.php:

<?php
require __DIR__.'/../pc_backend/bootstrap/autoload.php';
$app = require_once __DIR__.'/../pc_backend/bootstrap/start.php';
$app->run();

.htaccess: (unmodified)

.htaccess:(未修改)

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ / [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

There exists an almost identical question here on stackoverflow, but it does not provide an answer.

在 stackoverflow 上存在一个几乎相同的问题,但它没有提供答案。

How can I resolve this?

我该如何解决这个问题?

回答by hansn

After setting AllowOverride to all in apache2.conf and enabling mod_rewrite with the command a2enmod rewriteit all works.

在 apache2.conf 中将 AllowOverride 设置为 all 并使用命令启用 mod_rewrite 后,a2enmod rewrite一切正常。

回答by Zeshan

I understand this is an old question but want to post my solution here. It might help others.

我知道这是一个老问题,但想在这里发布我的解决方案。它可能会帮助其他人。

It is mainly the issue with apache virtual host. After the DocumentRoot, make sure you have the following script available in the vhost config file.

主要是apache虚拟主机的问题。在 DocumentRoot 之后,确保您在 vhost 配置文件中有以下脚本可用。

<Directory "/var/www/path/to/my/app/public">
        Options FollowSymLinks
        AllowOverride All

        Order allow,deny
        Allow from all
</Directory>

Another reason could be to enable apache rewrite mod. You can do it by using the following commands.

另一个原因可能是启用 apache 重写 mod。您可以使用以下命令来完成。

sudo a2enmod rewrite
sudo service apache2 restart

回答by AH Rasel

put

index.php

索引.php

and

.httaccess

.httaccess

in same directory.

在同一个目录中。

it's work for me

这对我有用