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

It's pretty clear that the problem appears because of the "mod_rewrite", in some cases just enabling this module in Apache is enough to get it fixed.

However, in my case, I had to extend the configuration for the VirtualHost in the following way:

<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>
查看更多
啃猪蹄的小仙女
3楼-- · 2020-01-24 12:26

It's like @GaryJ & @MartinGomez said. This is the content of the .htaccess that should be set on the public folder for all your laravel 4 projects running on Apache server:

<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>
查看更多
来,给爷笑一个
4楼-- · 2020-01-24 12:26

For Nginx users, you probably copied the the default virtualhost - but Laravel requires some customization on the location directive to allow the Laravel application to do the routing and not Nginx.

In your /etc/nginx/sites-available/your-site-name replace the location directive with this:

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

Then run sudo service nginx reload to make the changes come into effect.

查看更多
祖国的老花朵
5楼-- · 2020-01-24 12:27

Had exactly the same problem.

Two things I needed to do to fix this:

  1. enable rewrite_module in Apache
  2. Change the AllowOverride from None to All, example (Apache 2.4.9):

    Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted

FYI: use Laravel Homestead together with VirtualBox and Vagrant instead of WAMP. It contains Nginx instead of Apache but everything works by default as it is configured for Laravel explicitly.

查看更多
该账号已被封号
6楼-- · 2020-01-24 12:29

I had a similar problem on Ubuntu 14.04 with Lumen 5.4.

The solution was two parts for me. First in my vhost file /etc/apache2/sites-enabled/my_vhost.conf, I had to add the following <Directory> entry to allow overrides, after declaring my 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>

Next, I had to enable rewrites with:

sudo a2enmod rewrite
sudo service apache2 restart
查看更多
Rolldiameter
7楼-- · 2020-01-24 12:35

FOR UBUNTU USERS - tested for Ubuntu 18.04

1- Enable mod rewrite

sudo a2enmod rewrite

2- Change AllowOverride in apache conf file:

sudo nano /etc/apache2/apache2.conf

Change the AllowOverride from None to All in this block

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

3- Restart Apache

sudo service apache2 restart
查看更多
登录 后发表回答