Laravel 4 All Routes Except Home Result in 404 Err

2020-01-24 11:59发布

I installed Laravel 4 using Composer and also set up a virtual host. Currently, only the root route is working:

<?php

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

This is not:

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

What I'm trying to hit is TasksController at /tasks:

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

This is giving me 404 error as well. What could I be doing wrong? I have a default .htaccess file at the root of my project:

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

I am using localhost on a Mac.

18条回答
男人必须洒脱
2楼-- · 2020-01-24 12:19

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

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

查看更多
来,给爷笑一个
3楼-- · 2020-01-24 12:19

Go to

nano /etc/nginx/sites-available/yoursite

find try_file and replace as below:

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

查看更多
\"骚年 ilove
4楼-- · 2020-01-24 12:20

I tried all these with no success then i found another post and changed my .htaccess to this:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
</IfModule>
查看更多
Explosion°爆炸
5楼-- · 2020-01-24 12:21

I had the same problem and the solution was enable the rewrite mod on Apache2

In a terminal use the following commands:

$ sudo a2enmod rewrite
$ sudo service apache2 restart

And magic!

查看更多
三岁会撩人
6楼-- · 2020-01-24 12:23

There was a little flaw in a previous answer. This might help out.

$ sudo a2enmod rewrite
$ sudo service apache2 restart

Hope it does.

查看更多
姐就是有狂的资本
7楼-- · 2020-01-24 12:23

I had this problem (on Windows with manually installed Apache 2.2), and the cause was a missing AllowOverride in my VirtualHost as the root directory in httpd.conf was defaulted to None.

FileInfo Options=MultiViews is the minimal set you need for Laravel's .htaccess

e.g.

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

Or use AllowOverride All if this doesn't work and you don't care for security.

查看更多
登录 后发表回答