8

As the title says, all the routes in my laravel app except the home('/') route result in a 404 error.

I have separated the public folder from the rest of the laravel app as represented in the folder structure below. EDIT: I have confirmed that this is not what's causing the problem.

This error occurs on both the development system (local) and the production system (shared hosting).

EDIT: I forgot to mention: routes work if I go to localhost/index.php/route_name

Folder structure: (folder names changed to public/ and laravel/ for convenience)

.
+-- public/
|    +-- index.php
|    +-- packages/
|    +-- etc...
+-- laravel/
|    +-- app/
|    +-- artisan
|    +-- etc...

routes.php:

<?php
Route::get('/', function() // Only this route works
{
    return 'hello world';
});

Route::get('oversikt', function() // This route does not work
{
    return 'goodbye world';
});

bootstrap/paths.php:

<?php
return array(
    'app' => __DIR__.'/../app',
    'public' => __DIR__.'/../../pc',
    'base' => __DIR__.'/..',
    'storage' => __DIR__.'/../app/storage',
);

index.php:

<?php
require __DIR__.'/../pc_backend/bootstrap/autoload.php';
$app = require_once __DIR__.'/../pc_backend/bootstrap/start.php';
$app->run();

.htaccess: (unmodified)

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

    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

There exists an almost identical question here on stackoverflow, but it does not provide an answer.

How can I resolve this?

Community
  • 1
  • 1
hansn
  • 1,936
  • 3
  • 19
  • 32
  • 3
    Does your Apache configuration allow for `.htaccess` overrides like in my last answer: [index.php still needed, even after .htaccess](http://stackoverflow.com/questions/24146969/index-php-still-needed-even-after-htaccess/24147201#24147201) – Sam Jun 10 '14 at 17:38
  • Yes. I have also hosted a Laravel application on my shared host previously, without similar problems. – hansn Jun 10 '14 at 17:44
  • In your bootstrap/paths.php try changing the public to `'public' => __DIR__.'/../public'` – Mitch Jun 10 '14 at 17:45
  • That did not work, Mitch. – hansn Jun 10 '14 at 17:48
  • Everything was fine before separated the public folder? –  Jun 10 '14 at 18:07
  • I just confirmed, by creating a new laravel project, that it did not work before separating the public folder. It behaves exactly the same, on both systems. – hansn Jun 10 '14 at 20:59
  • 3
    Are you sure mod_rewrite is enabled on the server? Just trying to rule out the obvious... – Kryten Jun 10 '14 at 21:46
  • Yes @Kryten, I just confirmed that mod_rewrite is enabled using [this stackoverflow answer](http://stackoverflow.com/a/7137139/3719266) – hansn Jun 11 '14 at 06:41
  • For some reason the route started working on my production system! I will check mod rewrite (again) on the development system when I get home from work. – hansn Jun 11 '14 at 06:49

4 Answers4

12

After setting AllowOverride to all in apache2.conf and enabling mod_rewrite with the command a2enmod rewrite it all works.

hansn
  • 1,936
  • 3
  • 19
  • 32
  • 1
    How is this not an answer? Also @OP: flag it as the correct answer if it worked. – Sergiu Paraschiv Jun 11 '14 at 15:48
  • I think there's a good reply to this problem: [Why do I always get a 404 error for any route I create](https://laracasts.com/discuss/channels/laravel/why-do-i-always-get-a-404-error-for-any-route-i-create) – Amir Mojiry Feb 03 '20 at 11:53
7

I understand this is an old question but want to post my solution here. It might help others.

It is mainly the issue with apache virtual host. After the DocumentRoot, make sure you have the following script available in the vhost config file.

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

        Order allow,deny
        Allow from all
</Directory>

Another reason could be to enable apache rewrite mod. You can do it by using the following commands.

sudo a2enmod rewrite
sudo service apache2 restart
Zeshan
  • 2,496
  • 3
  • 21
  • 26
1

Even if this is a quite old question, the provided answers were not enough to solve entirely the problem.

  • Right : AllowOverride should be set at All.

  • but also, in the same config file (for some linux users, httpd.conf), uncomment the line (remove the #) :

    LoadModule rewrite_module modules/mod_rewrite.so

lionel
  • 196
  • 1
  • 5
0

put

index.php

and

.httaccess

in same directory.

it's work for me

AH Rasel
  • 161
  • 2
  • 15