【问题标题】:Laravel 4 Routes with ApacheLaravel 4 路由与 Apache
【发布时间】:2014-06-29 00:31:21
【问题描述】:

我刚开始学习 laravel 4,但我遇到了路线问题。当我转到我的根 url 时,路由似乎工作正常,但是当我尝试访问我的第二条路由时,我收到 404 错误,并且服务器日志显示“找不到文件”错误。

我设置了 AllowOverride ALL 并将原来的 .htaccess 代码更改为我在下面发布的代码。

注意: example.com 正在运行,它会按预期返回 All cats

这是代码:

路线

Route::get('/', function(){
    return "All cats";
});

Route::get('cats/{id}',function($id){
    return "Cat #$id";
})->where('id','[0-9]+');

.htaccess

<IfModule mod_rewrite.c>
     RewriteEngine on

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

     RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

httpd.conf

<VirtualHost *:80>
        ServerAdmin admin@example.com
        DocumentRoot /var/www/html/cats/public
        ServerName example.com
        ErrorLog logs/example.com-error_log
        CustomLog logs/example.com-access_log common
</VirtualHost>

尝试访问 example.com/cats/123 时出现 HTML 错误

The requested URL /cats/123 was not found on this server.

错误日志

 File does not exist: /var/www/html/cats/public/cats

【问题讨论】:

  • 为什么不能使用 Laravel 提供的原始 .htaccess 呢?
  • 我正在使用它,但它不起作用,我看到很多人都在改用我上面发布的那个。
  • 改回原来的 htaccess 并在你的虚拟主机配置中添加 AllowOverride All。
  • 我尝试在 VH 中添加 AllowOverride ALL,但出现错误,所以我将其留在原处。
  • 在 VH 配置中添加 Allowoverride 时遇到什么错误?

标签: apache laravel laravel-4


【解决方案1】:

AllowOverride 指令是针对每个目录的,因此将其随机放在 Apache 的配置文件中是行不通的。

在你的 Laravel 安装中为 public 目录创建一个新的 Directory 块,并将 AllowOverride All 放在那里,如下所示:

<Directory "/path/to/your/laravel/public">
    AllowOverride All
</Directory>

此外,如果您只托管单个 Laravel 安装,则无需像您一样使用虚拟主机,只需将现有的 DocumentRoot 指令和 Directory 块更改为都指向您的 Laravel 的公共目录,并且在该目录块中添加AllowOverride All

【讨论】:

    猜你喜欢
    • 2013-01-15
    • 2015-10-20
    • 2013-05-22
    • 2013-05-27
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多