90

I've just started learning the Laravel framework and I'm having an issue with routing.

The only route that's working is the default home route that's attached to Laravel out of the box.

I'm using WAMP on Windows and it uses PHP 5.4.3, and Apache 2.2.22, and I also have mod_rewrite enabled, and have removed the 'index.php' from the application.php config file to leave an empty string.

I've created a new controller called User:

class User_Controller extends Base_Controller {
    public $restful = true;

    public function get_index() 
    {
        return View::make('user.index');
    }
}

I've created a view file in application/views/user/ called index.php with some basic HTML code, and in routes.php I've added the following:

Route::get('/', function () {
    return View::make('home.index');
});

Route::get('user', function () {
    return View::make('user.index');
});

The first route works fine when visiting the root (http://localhost/mysite/public) in my web browser, but when I try to go to my second route with http://localhost/mysite/public/user I get a 404 Not Found error. Why would this be happening?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
JasonMortonNZ
  • 3,752
  • 8
  • 34
  • 56

19 Answers19

144

On my Ubuntu LAMP installation, I solved this problem with the following 2 changes.

  1. Enable mod_rewrite on the apache server: sudo a2enmod rewrite.
  2. Edit /etc/apache2/apache2.conf, changing the "AllowOverride" directive for the /var/www directory (which is my main document root): AllowOverride All

Then restart the Apache server: service apache2 restart

Gowri
  • 16,587
  • 26
  • 100
  • 160
Andrew Vickers
  • 2,504
  • 2
  • 10
  • 16
  • This fixed this issue for me on a vagrant machine downloaded from puphpet. – winkbrace Feb 04 '15 at 22:08
  • Worked for me on MacOS High Sierra 10.13.3 with Laravel-5.4 – Yevgeniy Afanasyev Mar 05 '18 at 04:30
  • 2
    @McSonk, probably question is for windows. This alternative solution works for Ubuntu. Thank dude. – Fendi Septiawan Jul 11 '18 at 03:31
  • The apache2 config file seems to take precedence over the virtualhost file. If you have `AllowOverride All` in your virtualhost file the website may be using whatever value you have in your apache config file instead. – sdexp Aug 27 '18 at 18:30
  • That's a great answer if you have a local apache or virtual machine. But in real life, your web is in a hosting, and you don't have any access to the apache config. There must be another solution. – Alberto Torre Dec 15 '18 at 21:12
63

Using WAMP click on wamp icon ->apache->apache modules->scroll and check rewrite_module.

Restart a LoadModule rewrite_module

Note: the server application restarts automatically for you once you enable "rewrite_module"

Mario Uvera
  • 851
  • 1
  • 7
  • 7
  • THANKS A LOT, been having trying to sort this issue since yesterday – Moe Nov 24 '13 at 12:29
  • 2
    This seems to happen to me every time I install/reinstall WAMP and Laravel on a computer. This is always the issue. Hopefully it will finally be committed to memory ;) – Kenmore Dec 13 '13 at 07:53
  • Under Ubuntu 14.04, 'sudo a2enmod rewrite' and then 'sudo service apache2 restart' worked. – nspo Sep 24 '14 at 08:54
  • for linux systems enable rewrite module ```sudo a2enmod rewrite``` after that restart ```sudo service apache2 restart``` – aimme Jul 19 '16 at 19:53
38

Have you tried to check if

http://localhost/mysite/public/index.php/user 

was working? If so then make sure all your path's folders don't have any uppercase letters. I had the same situation and converting letters to lower case helped.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
user1930566
  • 381
  • 3
  • 2
  • Thanks. I'm going through the Laravel book (Laravel Starter) step by step and was stuck with routing to a closure (page 13). This resolved that problem for me. – Steve Lindsey Feb 05 '13 at 19:33
  • This solved my problem. I had a .htaccess in the root of my apache folder that broke laravel's htaccess. – Maarten00 Dec 11 '13 at 10:51
  • This solved other problem I had. Using Laravel to generate pages, and Angular to call REST, on Windows the call to `api/branches` worked, but on linux, I had to change this url like : `index.php/api/branches` Any way to resolve this to have the same urls ? – miroslavign Sep 23 '15 at 09:57
  • I never thought it's case-sensitive – ClearBoth May 23 '17 at 05:35
  • instead place .htaccess file from public/ folder to the root of project, then you can access by `http://localhost/mysite/user` – Rohan Khude Jan 10 '18 at 09:56
  • I have all my paths in lowercase, and still not working. /public/index.php/user works, /public/user returns 404 – Alberto Torre Dec 16 '18 at 13:22
25

Have you tried adding this to your routes file instead Route::get('user', "user@index")?

The piece of text before the @, user in this case, will direct the page to the user controller and the piece of text after the @, index, will direct the script to the user function public function get_index().

I see you're using $restful, in which case you could set your Route to Route::any('user', 'user@index'). This will handle both POST and GET, instead of writing them both out separately.

PapaSmurf
  • 1,035
  • 2
  • 14
  • 26
  • I change from WAMP to XAMPP because the issue remained. After starting a fresh project on XAMPP server and using the '@' symbol instead of the '.' it working great now. – JasonMortonNZ Aug 04 '12 at 02:46
  • 1
    It works fine with WAMP, you just have to enable rewrite_module. See @Muvera's comment below. – ajon Jan 03 '15 at 18:21
  • 3
    I know this is an old answer, but I had a similar issue: a new route was added and did not work! (404). I only needed to clear cached routes! using php artisan route:clear – trainoasis Dec 22 '15 at 08:27
9

I was getting the same problem using EasyPHP. Found that I had to specify AllowOverride All in my <Directory> block in httpd.conf. Without this, Apache sometimes ignores your .htaccess.

Mine ended up looking like this...

<Directory "D:/Dev">
    Options FollowSymLinks Indexes
    #### NEXT IS THE CRUCIAL LINE ####
    AllowOverride All                  
    Order deny,allow
    Allow from 127.0.0.1
    Deny from all
    Require all granted     
</Directory>
Simon East
  • 55,742
  • 17
  • 139
  • 133
  • I found on Debian GNU/Linux 7.6, that this response gave part of what was required, in addition to enabling the module for apache2 (a2enmod rewrite). – Kevin Buchs Sep 08 '14 at 19:16
6

You could try to move root/public/.htaccess to root/.htaccess and it should work

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Wasim A.
  • 9,660
  • 22
  • 90
  • 120
  • 4
    You posted the exact same answer to 8 questions. If you think they are duplicates, you should mark them as such and not post an answer to each. – Jaap Dec 22 '16 at 19:45
6

Just Run in your terminal.

php artisan route:clear
5

Routes

Use them to define specific routes that aren't managed by controllers.

Controllers

Use them when you want to use traditional MVC architecture

Solution to your problem

You don't register controllers as routes unless you want a specific 'named' route for a controller action.

Rather than create a route for your controllers actions, just register your controller:

Route::controller('user');

Now your controller is registered, you can navigate to http://localhost/mysite/public/user and your get_index will be run.

You can also register all controllers in one go:

Route::controller(Controller::detect());
David Barker
  • 14,484
  • 3
  • 48
  • 77
4

OK, so after bashing my head on this problem for a little over a day... I got up and did what I SHOULD have done yesterday, and DEBUGGED what was going on!

What Laravel is TRYING to do here, is insert the file index.php right in front of the path given as a Route. SO for instance, if you specified a Route::get('/account/create', ..., and execute your app from say localhost/laravel/authenticate/public/account/create on your browser, then laravel wants to execute localhost/authenticate/public/index.php/account/create, but to do that.... Apache needs to see that requests through /wamp/www/laravel/laravel/authentication/public (your path may vary somewhat, depending on where your laravel app is actually installed, but the trailing public is where the substitution needs to take place) must have a 'RewriteRule' applied.

Thankfully, laravel supplies the correct Rewrite rule in a handy .htaccess file right there in your app's public folder. The PROBLEM is, the code in that '.htaccess' file won't work with the way WAMP is configured out of the box. The reason for this SEEMS to be the problem suggested by muvera at the top of this thread -- the rewrite_module code needs to be loaded by Apache before the RewriteRule stuff will work. Heck this makes sense.

The part that DOESN'T make sense: simply stopping and restarting Apache services will not pick up the changes necessary for WAMP to do the right thing with your RewriteRule -- I know, I tried this many times!

What DOES work: make the changes suggested by muvera (see top of thread) to load the correct modules. Then, reset your whole Windows session, thus dumping Apache out of memory altogether. Restart (reload) WAMP, and VOILA! the fix works, the correct RewriteRule is applied, yada, yada; I'm living happily ever after.

The good news out of all this: I know a LOT more about .htaccess, RewriteRule, and httpd.conf files now. There is a good (performance) argument for moving the logic from your app's public .htaccess file, and putting it into a Directory ... section of your httpd.conf in your Apache 'bin' folder BTW (especially if you have access to that folder).

ProGM
  • 6,949
  • 4
  • 33
  • 52
mdg
  • 546
  • 4
  • 4
3

Don't forget the RewriteBase in your public/.htaccess:

For example:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /your/folder/public
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
0

Try enabling short php tags in your php.ini. WAMP has them off usually and laravel needs them on.

John Conde
  • 217,595
  • 99
  • 455
  • 496
dajavax
  • 3,060
  • 1
  • 14
  • 14
0
Route::get('/', function() {
    return View::make('home.index');
});
    
Route::get('user', function() {
    return View::make('user.index');
});

change above to

Route::get('user', function() {
    return View::make('user.index');
});
    
Route::get('/', function() {
    return View::make('home.index');
});

You have to use '/'(home/default) at the end in your routes

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
athula
  • 31
  • 1
0

you must be using Laravel 5 the command

  class User_Controller extends Controller {
  public $restful = true;
  public function get_index(){
  return View('user.index');
  }
  }

and in routes.php

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

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

Laravel 5 command changes for view and controller see the documentation i was having same error before

0

Just Run in your terminal.

 composer dump-autoload
Bakhtawar GIll
  • 407
  • 5
  • 16
0

The Main problem of route not working is there is mod_rewrite.so module in macos, linux not enabled in httpd.conf file of apache configuration, so can .htaccess to work. i have solved this by uncomment the line :

# LoadModule rewrite_module libexec/apache2/mod_rewrite.so

Remove the # from above line of httpdf.conf. Then it will works.
enjoy!

M. Rostami
  • 999
  • 3
  • 16
  • 27
0

I think you have deleted default .htaccess file inside the laravel public folder. upload the file it should fix your problem.

jewelhuq
  • 1,210
  • 15
  • 19
0

the simple Commands with automatic loads the dependencies

composer dump-autoload

and still getting that your some important files are missing so go here to see whole procedure

https://codingexpertise.blogspot.com/2018/11/laravel-new.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34
0

If you're using Vagrant though Homestead, it's possible there was an error mounting the shared folder. It looks like Vagrant takes your files from that folder and swaps out the files that are actually on the host machine on boot, so if there was an error, you're essentially trying to access your Laravel installation from when you first made it (which is why you're only getting "home"- that was generated during installation).

You can easily check this by sshing into your vm and checking the routes/web.php file to see if it's actually your file. If it isn't, exit out and vagrant halt, vagrant up, and look for errors on boot.

0
  1. setup .env file
  2. configure index.html
  3. make sure u have .htaccess
  4. sudo service apache2 restart

most probably it's due to cache problems

Abhi
  • 119
  • 1
  • 2