73

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.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Amit Erandole
  • 11,995
  • 23
  • 65
  • 103
  • 7
    Amit: did you solve your problem above? If yes could please post your solution because I am facing same problem with my laravel project. Thanks in advance – Om3ga Jan 24 '13 at 11:33
  • @AmitErandole Whoops. We seem to have missed the gist of that gist.github.com/4131966 you posted. – itsazzad Feb 23 '15 at 07:55

19 Answers19

117

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

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

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
GaryJ
  • 1,261
  • 1
  • 7
  • 3
  • 2
    crap! /index.php/hello works but /index.php/tasks gives me a 500 error now. WTF? I'll update my question with the htaccess contents – Amit Erandole Nov 22 '12 at 14:57
  • 1
    I don't get it: I have mod rewrite on and AllowOverride is set to All too – Amit Erandole Nov 22 '12 at 15:00
  • 2
    Check if *anything* in .htaccess is working - if you're running Apache 2.4, note the changes since previous versions, regarding `Require all granted` and `AllowOverride all` within a `...` block on your virtual host. And reboot Apache for luck. – GaryJ Nov 22 '12 at 15:04
  • rebooted and everything checks out fine but still no luck with the .htaccess file – Amit Erandole Nov 22 '12 at 15:16
  • Try removing the IfModule conditional. As you've got access to the host / vhost, you can soon enable that module if it's not - so it doesn't need be checked on every request. Equally, try moving it out of .htaccess, and into a `...` block in your vhost - if you've got nothing else in your .htaccess it can then be deleted as well. – GaryJ Nov 22 '12 at 15:19
  • My http-vhosts block looks like this: DocumentRoot "/Users/amiterandole/Sites/laravelbackbone/public" ServerName laravelbackbone.dev What exactly should I move into this? – Amit Erandole Nov 22 '12 at 15:25
  • Try: DocumentRoot "/Users/amiterandole/Sites/laravelbackbone/public" ServerName laravelbackbone.dev AllowOverride all Require all granted Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] – GaryJ Nov 22 '12 at 15:29
  • I emptied out my htaccess file and tried the above and now nothing seems to work. Now laravelbackbone.dev points to my Sites folder root for some reason (?!) – Amit Erandole Nov 22 '12 at 15:42
  • You've definitely got the `conf/extra/httpd-vhosts.conf` file be included (uncommented) within the main `httpd.conf`? – GaryJ Nov 22 '12 at 15:55
  • probably not. Where do I find this line? – Amit Erandole Nov 22 '12 at 16:17
  • Here is my entire http.conf file for your perusal: http://d.pr/n/tKdp Do you see anything wrong here? – Amit Erandole Nov 22 '12 at 16:22
  • Please help? I've got the same vhost set up (obviously different DocumentRoot), I've also emptied (and tried deletion of) the .htaccess file, I'm including conf/extra/httpd-vhots.conf in my main httpd.conf file. I'm up to the point where Amit said laravelbackbone.dev points to his Sites folder root, except, I get a Bad Request. Please see my question here: http://stackoverflow.com/questions/18859352/laravel-4-routing-not-working-due-to-htaccess-file – marienke Sep 17 '13 at 20:33
  • @AmitErandole Whoops. We seem to have missed the gist of that https://gist.github.com/4131966 you posted. – itsazzad Feb 23 '15 at 07:54
40

Had the same problem running Laravel 4 on WAMP (Windows 8).
The solution that worked for me was:

  1. Open apache httpd.conf and find this line :

    #LoadModule rewrite_module modules/mod_rewrite.so
    
  2. Uncomment this line (remove the #)
  3. Save httpd.conf
  4. Restart WAMP

It should be working!

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
Rutger
  • 401
  • 4
  • 2
  • I don't have #LoadModule rewrite_module modules/mod_rewrite.so in my httpd.conf :( is that a Windows8 thing? I'm using XAMPP on Windows7 – marienke Sep 17 '13 at 18:40
  • 13
    On Ubuntu, this would be: `sudo a2enmod rewrite` followed by `sudo service apache2 restart` – Jeff Lambert Sep 28 '13 at 18:39
  • Perfect! This fixed my problem. – Captain Hypertext Nov 13 '15 at 03:14
  • I just want to add that you might need to set `AllowOverride FileInfo` to the directory in httpd.conf or just move the rewrite directives to that directory segment and forego the `.htaccess` file – hanzo2001 Nov 19 '16 at 22:14
30

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!

hibob
  • 746
  • 7
  • 17
manujas
  • 301
  • 3
  • 4
20

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

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

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.

Qirel
  • 25,449
  • 7
  • 45
  • 62
Orhan
  • 1,395
  • 13
  • 12
  • this worked for me when deploying on my server. Vagrant is a good suggestion, more explicitly I'd suggest either http://laravel.com/docs/4.2/homestead or https://box.scotch.io/ if you want to stick to Apache – Luca Feb 08 '15 at 11:16
  • AllowOverride worked for me with fresh xampp install and vhost – Can Tecim Jun 23 '15 at 18:58
  • I had the exact same issue. I also performed the "sudo a2enmod rewrite" command for apache, and then changed AllOverride from None to All in the apache2.conf file. – Richard Chassereau Feb 27 '16 at 06:45
16

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
BassMHL
  • 8,523
  • 9
  • 50
  • 67
12

Even after enabling mod_rewrite, you may face this problem if you are aliasing your laravel/public folder.

Adding

RewriteBase /your_apache_alias

to .htaccess does the trick.

MartinGomez
  • 121
  • 2
  • 3
9

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>
Dylan Glockler
  • 1,115
  • 1
  • 20
  • 40
6

You don't need a / when defining anything other than home:

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

Should work.

davzie
  • 116
  • 4
5

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>
Heroselohim
  • 1,241
  • 1
  • 18
  • 23
  • 1
    Good lord.. this is the ONLY thing that worked after hours of doing everything else imaginable. I am using Laravel 7... it's been nightmare trying to run some Laravel apps and installing. I wish they would have better error handling to let people know what's wrong on the page if debug is turned on. Maybe it's because I am installing an older Laravel app. Seems really difficult process. – Scott Yu - builds stuff Mar 15 '20 at 22:29
4

Since Laravel 4 is autoloading files from a map in a static file, you need to update that file when you add a new controller. Run this command to rebuild the static file:

php composer.phar dump-autoload
Niklas Modess
  • 2,521
  • 1
  • 20
  • 34
4

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

$ sudo a2enmod rewrite
$ sudo service apache2 restart

Hope it does.

Caleb Mbakwe
  • 178
  • 1
  • 6
3

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.

scipilot
  • 6,681
  • 1
  • 46
  • 65
3

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>
Samsquanch
  • 8,866
  • 12
  • 50
  • 89
2

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.

Dylan Pierce
  • 4,313
  • 3
  • 35
  • 45
2

Go to

nano /etc/nginx/sites-available/yoursite

find try_file and replace as below:

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

Murali Krishna
  • 93
  • 1
  • 11
1

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
The Unknown Dev
  • 3,039
  • 4
  • 27
  • 39
0

This update worked for me. In the .htaccess file simply add the following after the Rewriterules are turned on.

Options -Indexes
Options +FollowSymLinks

Worked like charm

0

If using IIS as web server, create a file with this name web.config on public folder or where index.php is with this configuration :

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

If you using apache edit .htaccess file and add this part

<Directory />
    AllowOverride All
</Directory>

In linux and nginx there isn't this problem

jinglebird
  • 558
  • 1
  • 5
  • 15
-1

This comment may be a little late but it may help. I had the same problem when routing to another view like this:

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

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

But didn't work so I tried everything i found in all the related posts but wasn't enough to solve my problem so i found this lines on the httpd.conf:

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

So i changed "denied" to "granted" and also commented this line on my .htaccess:

#RewriteBase /

And worked!! I think this lines make Apache not consider the .htaccess file of your project so turning it to granted made the difference. Hope this can help someone with the same problem.

Working on Apache Server 2 @ Manjaro Linux (based on Archlinux)