38

Ok, I'm new to Laravel so went straight to the documentation to get started. There are massive holes in the documentation so it took a lot of effort and googling to fill the gaps to get Laravel set up. I now have it set up and moved on to the next step in the quick start guide. I created my route

Route::get('users', function()
{
     return 'Users!';
});

Now it says:

Now, if you hit the /users route in your web browser, you should see Users!

So I hit up:

http://localhost/laravel/users 

but get a 404? I tried

http://localhost/laravel/public/users 

but still a 404? I followed the steps on the quick start guide to the letter, what am I missing?

9
  • 1
    Are you certain that laravel is there? Can you access anything else in public? Jun 3, 2013 at 13:22
  • yes I can access the welcome page 'you have arrived' default page, at this address localhost/laravel/public
    – twigg
    Jun 3, 2013 at 13:29
  • 8
    What about localhost/laravel/public/index.php/users ? Jun 3, 2013 at 13:38
  • 1
    It sounds like your htaccess/rewrite isn't working then. Tripple check that you have mod rewrite turned on, and that you've got AllowOverride configured correctly. I would recommend that before too long you look in to virtual hosts too. Jun 3, 2013 at 16:36
  • 8
    I really don't understand why LAravel has 450.000 downloads (!) but nobody except us has this problem! Laravel's tutorial is horrible, when you do it exactly like they same you'll run into exactly that error...
    – Sliq
    Nov 3, 2013 at 20:47

9 Answers 9

75

Seems like your Laravel app is accesible via an Apache HTTP alias, because your URL looks like: http://localhost/laravel/. If this is the case and assuming that http://localhost/laravel is pointing to your public directory, then follow these steps:

  1. Try to navigate to your expected route prepend it with /index.php/, in your case: http://localhost/laravel/index.php/users. If it works (no 404) then you problem is with the Rewrite Module configuration of Apache HTTP, you should follow the next steps.
  2. Edit the file public/.htaccess.
  3. Under the line RewriteEngine On add RewriteBase /laravel/.
  4. Try to navigate to an existing route.

Basically, if you app resides in a alias or virtual directory (say http://localhost/alias) you should add an entry in your rewrite rule to rewrite the base directory with alias.

10
  • 2
    Hi Rubens, I think I'm getting close on this now. localhost/laravel/index.php/users dosn't work,but if I put public in the URL it does work like so localhost/laravel/public/index.php/users The rewritebase didn't work but im guessing I have to incorporate the /public directory into it somewhere?
    – twigg
    Jun 3, 2013 at 16:08
  • 1
    Are you using WAMP? Then go to tray icon > click > Apache > Alias directories > Add an alias... then write an alias like test the set the directory as C:\path\to\your\public\directory then navigate to http://localhost/test. Jun 3, 2013 at 16:43
  • 1
    This exact solution (Rewritebase...) worked on my Laravel installation under WAMP. My alias is /localhost/laravel-demo/ Thx.
    – Dizzley
    Jan 17, 2014 at 5:22
  • 6
    Be sure that mod_rewrite in httpd.conf is enabled. See stackoverflow.com/a/16864142/1532882.
    – Jake Z
    Apr 9, 2014 at 19:21
  • 1
    This was very helpful. I had the issue you described.
    – AturSams
    May 27, 2014 at 15:27
5

The problem is well explained by Rubens above, A simpler solution is to use the supplied built-in PHP Server by issuing this command

php artisan serve --port=8080

Note that I am using port 8080 here, you can omit it.Now you can browse the site by going to

localhost/users

and it should work!

3

Apache isn't probably reading the public/.htaccess file due to the directive AllowOverride being set to None. Try to set it to All.

Laravel 4 simple route not working using mod_rewrite, and .htaccess

2

I had the same problem and spent hours to solve it. I have several apps under the same domain, but in different segments. So Laravel's url is http://myhostname/mysubdir/

My controllers were only working as http://myhostname/mysubdir/index.php/mycontroller

On /var/www/.../public/.htaccess I put RewriteBase /mysubdir and worked!!!

1

I know this question is 4 years old but still it have its significance.Rubens Mariuzzo was answered it correctly but I want to add some points on it. You said

"There are massive holes in the documentation so it took a lot of effort and googling to fill the gaps in order to get Laravel set-up"

For beginners it is difficult to find correct way of configuring Laravel. Once it is mastered it is fun developing Laravel :) . There are certain correct way of doing this.

  1. Download Laravel
  2. Configure DB
  3. Map DB in .env
  4. Make auth: php artisan make:auth
  5. Create model and migration together: php artisan make:model Todo -m
  6. Migrate: php artisan migrate
  7. Create controller and routes together: php artisan make:controller TodoController --resource
  8. Create view for each action
  9. Code the controller

Detailed description is given in this blog http://masterlaravel.blogspot.in/2017/08/laravelquick-start-composer-create.html

0

please refer to this follow url and see RoboTamer's config:

http://forums.laravel.io/viewtopic.php?id=511

it solved my problem the same as yours

solution in nginx:

server {

    server_name .laravel.dev;
    root /home/tamer/code/laravel/public;

    index index.php index.html;

    #browse folders if no index file
    autoindex on; 

    # serve static files directly
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }


    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/lobby(/index)?|/index(.php)?)/?$)
    {
        rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # catch all
    error_page 404 /index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/tmp/php.socket;
        fastcgi_index index.php;
        #include fastcgi_params;
        include /home/tamer/code/nginx/fastcgi_params;
    }
    access_log /home/tamer/code/laravel/storage/logs.access.log;
    error_log  /home/tamer/code/laravel/storage/logs.error.log;

}

2
  • 1
    Please summarize the answer here, in case the site goes offline.
    – Evan
    Oct 29, 2013 at 3:56
  • Note: the linked article describes a solution for an installation under Nginx. Rubens answer is good for Apache.
    – Dizzley
    Jan 17, 2014 at 5:35
0

Thanks. Knowledge of the above by @rubens, and a comment by @GaryJ here made me do this to make it work from my Apache virtual hosts file.

Copied these lines from .htaccess file in laravel within node of the vhosts config. Removed my .htaccess file (it was anyway not being applied, i am on a windows apache virtual host, laravel env)

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]

Better: Later read @Ag0r0 's reply, that worked. i was missing the allow override, making the above neccessary. once allowoverride all was there, the default settings itself worked.

0

In my case (Ubuntu 14.04 LTS & Apache 2.2) I was stuck at #1 step of @Rubens' solution. I could see the page with this url: http://localhost/laravel/index.php/users but step #2 and others didn't work.

Also I've tried to add RewriteBase /laravel/public to .htaccess but it didn't work too.

Then I've set up a new virtual host with the help of this awesome tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts (they rock with these tuts)

In addition to tutorial, I've set DocumentRoot (in example.com.conf file) like this:

DocumentRoot /var/www/laravel/public

Yes, public is the big deal here.

Btw, don't put RewriteBase /laravel/ to your .htacces file otherwise you'll get HTTP/500 error.

Now, I can see example.com/users page.

-3

my previous post with the describtion of the problem was deleted, but it was similar to the situation that is described here.

anyway... simply i have tried all the possibilities that are described here as well as on other pages and I did not find solution to my problem.

The fresh installation of the Laraval simply did not worked.... after trying everythink from this post i decided to create yet another fresh installation and miracle happened this installation is actually working...

So my advice is: if you are not able to fix this problem just try to create another installation of the laravel and it might actually work.

This might be actually the easiest way how to fix your problem in case you are starting from the scrach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.