70

我使用 Composer 安装了 Laravel 4 并设置了一个虚拟主机。目前,只有根路由有效:

<?php

Route::get('/', function()
{
    return View::make('hello');
});

这不是:

Route::get('/hello', function()
{
    return View::make('hello');
});

我要打的TasksController/tasks

Route::resource('tasks', 'TasksController');

这也给了我 404 错误。我可能做错了什么?我在项目的根目录中有一个默认的 .htaccess 文件:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我在 Mac 上使用 localhost。

4

19 回答 19

112

Just for a laugh, see if /index.php/hello works.

If so, then most likely it's a .htaccess problem.

于 2012-11-22T14:54:41.703 回答
40

在 WAMP(Windows 8)上运行 Laravel 4 时遇到了同样的问题。
对我有用的解决方案是:

  1. 打开 apache httpd.conf 并找到这一行:

    #LoadModule rewrite_module modules/mod_rewrite.so
    
  2. 取消注释此行(删除#
  3. 节省httpd.conf
  4. 重启 WAMP

它应该工作!

于 2013-05-31T19:04:27.133 回答
30

我有同样的问题,解决方案是在 Apache2 上启用重写模块

在终端中使用以下命令:

$ sudo a2enmod rewrite
$ sudo service apache2 restart

还有魔法!

于 2014-10-22T12:35:38.830 回答
18

有完全相同的问题。

我需要做两件事来解决这个问题:

  1. 在 Apache 中启用rewrite_module
  2. 将AllowOverride从 None更改为All,例如(Apache 2.4.9):

    <Directory "c:/www">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    

仅供参考:
将 Laravel Homestead 与 VirtualBox 和 Vagrant 一起使用,而不是 WAMP。它包含 Nginx 而不是 Apache,但默认情况下一切正常,因为它是为 Laravel 明确配置的。

于 2015-01-17T11:22:58.607 回答
16

为 UBUNTU 用户- 为 Ubuntu 18.04 测试

1-启用模组重写

sudo a2enmod rewrite

2-更改 apache conf 文件中的 AllowOverride:

sudo nano /etc/apache2/apache2.conf

在此块中将 AllowOverride 从 None 更改为 All

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

3-重新启动Apache

sudo service apache2 restart
于 2018-08-28T01:30:21.527 回答
12

即使在启用 mod_rewrite 之后,如果您为 laravel/public 文件夹设置别名,您也可能会遇到此问题。

添加

RewriteBase /your_apache_alias

到 .htaccess 就可以了。

于 2013-09-14T02:01:11.850 回答
8

我尝试了所有这些都没有成功,然后我找到了另一个帖子并将我的 .htaccess 更改为:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>
于 2015-03-08T06:32:15.560 回答
6

在定义 home 以外的任何内容时,您不需要 / :

Route::get('hello', function()
{
    return View::make('hello');
});

应该管用。

于 2012-11-22T14:45:22.977 回答
5

就像@GaryJ 和@MartinGomez 所说。这是.htaccess的内容,应该为在 Apache 服务器上运行的所有 laravel 4 项目的public文件夹设置:

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

    RewriteEngine On
    RewriteBase /

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
于 2014-11-13T12:49:06.883 回答
4

由于 Laravel 4 是从静态文件中的地图自动加载文件,因此您需要在添加新控制器时更新该文件。运行以下命令重建静态文件:

php composer.phar dump-autoload
于 2012-11-24T14:47:14.507 回答
4

之前的回答有一点小瑕疵。这可能会有所帮助。

$ sudo a2enmod rewrite
$ sudo service apache2 restart

希望确实如此。

于 2015-01-15T14:02:07.157 回答
3

很明显,问题是由于“mod_rewrite”而出现的,在某些情况下,只需在 Apache 中启用此模块就足以修复它。

但是,就我而言,我必须通过以下方式扩展 VirtualHost 的配置:

<VirtualHost *:80>

    ServerName adplus.local

    ServerAdmin sergey.donchenko@gmail.com
    DocumentRoot /var/www/adplus.local/project/public

    **<Directory "/var/www/adplus.local/project/public">
        AllowOverride All
    </Directory>**

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
于 2015-01-28T07:15:59.873 回答
3

我遇到了这个问题(在手动安装 Apache 2.2 的 Windows 上),原因是AllowOverride我的 VirtualHost 中缺少,因为 httpd.conf 中的根目录默认为 None。

FileInfo Options=MultiViews是 Laravel 的 .htaccess 所需的最小集合

例如

<VirtualHost *:80>
    DocumentRoot "C:/htdocs/domain.com/laravel/public"
    ServerName foo.domain.com

    <Directory "C:/htdocs/domain.com/laravel/public">
        AllowOverride FileInfo Options=MultiViews
    </Directory>
</VirtualHost>

或者AllowOverride All如果这不起作用并且您不关心安全性,请使用。

于 2014-08-02T06:57:10.563 回答
2

纳米 /etc/nginx/sites-available/yoursite

找到 try_file 并替换如下:

try_files $uri $uri/ /index.php?$query_string;

于 2016-12-23T15:04:04.153 回答
2

对于 Nginx 用户,您可能复制了default虚拟主机 - 但 Laravel 需要对location指令进行一些自定义,以允许 Laravel 应用程序而不是 Nginx 进行路由。

在你/etc/nginx/sites-available/your-site-name用这个替换位置指令:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

然后运行sudo service nginx reload以使更改生效。

于 2016-12-21T13:38:18.400 回答
1

我在带有 Lumen 5.4 的 Ubuntu 14.04 上遇到了类似的问题。

解决方案对我来说是两部分。首先在我的 vhost 文件/etc/apache2/sites-enabled/my_vhost.conf中,我必须在声明我的之后添加以下<Directory>条目以允许覆盖DocumentRoot

    DocumentRoot /var/www/path/to/my/app/public

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

            Order allow,deny
            Allow from all
    </Directory>

接下来,我必须启用重写:

sudo a2enmod rewrite
sudo service apache2 restart
于 2017-08-27T14:51:52.430 回答
0

此更新对我有用。在 .htaccess 文件中,只需在打开 Rewriterules 后添加以下内容。

Options -Indexes
Options +FollowSymLinks

像魅力一样工作

于 2015-01-15T02:08:07.513 回答
0

如果使用 IIS 作为 Web 服务器,请在 public 文件夹或 index.php 使用此配置的位置创建一个名为 web.config 的文件:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Imported Rule 1" stopProcessing="true">
                    <match url="^" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

如果您使用 apache 编辑 .htaccess 文件并添加此部分

<Directory />
    AllowOverride All
</Directory>

在linux和nginx中没有这个问题

于 2021-10-17T19:26:17.940 回答
-1

此评论可能有点晚,但可能会有所帮助。像这样路由到另一个视图时,我遇到了同样的问题:

Route::get('index', function () {
return view('index');
});

Route::get('login', function () {
return view('login');
});

但是没有用,所以我尝试了在所有相关帖子中找到的所有内容,但还不足以解决我的问题,所以我在 httpd.conf 上找到了以下几行:

<Files ".ht*">
Require all denied
</Files>

所以我将“拒绝”更改为“授予”,并在我的 .htaccess 中评论了这一行:

#RewriteBase /

并工作!我认为这些行使 Apache 不考虑您项目的 .htaccess 文件,因此将其设置为授予会有所不同。希望这可以帮助有同样问题的人。

在 Apache Server 2 @ Manjaro Linux 上工作(基于 Archlinux)

于 2016-09-13T22:45:55.430 回答