87

I'm using PhpStorm. I can run and open the index.php, but when I want to press submit button (post sign in), its display 404 not found.

Web server Apache 2.4 running on Windows 10.

This is my home

index.php

This is my route

web.php

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • I think you should probably go through the documentation of Laravel. Go through it https://laravel.com/docs/5.4/installation – VijayRana May 01 '17 at 06:32
  • What is the URL that you see in the browser's address bar when this page does not work? – LazyOne May 01 '17 at 08:42
  • You should include exact error and code of the form you try to submit – Marcin Nabiałek May 01 '17 at 08:48
  • 1
    another thing even though it looks not to be that here : if you have a route defined like /planning/{weeknumber} and then another like /planning/day, you can't reach the /planning/day unless you declare it before the other, because 'day' will be passed as a variable to the 1st route, and you might get a 404, depending on how your controller manages it. – Emmanuel BRUNO Mar 06 '19 at 14:22
  • 2
    Can you provide more information? Which route is the form sent to? – Nico Haase May 10 '19 at 14:41
  • 1
    I was finally able to solve the problem by running the command `php artisan route:clear` – VijayRana Oct 20 '21 at 03:42

35 Answers35

133

I'm not entirely sure why all the down-votes, especially since this is a common issue and the cause can be hidden for someone new to the Laravel environment. I know this is an old post, but I add it here for future newbies.

The first thing I suggest trying is running php artisan route:list from the command line (from your project root directory). This will list all the routes that Laravel can register and if there are any errors, usually they will show up here.

The second thing I suggest is to ensure that the URL matches route. I can't tell you how many times I've tried to figure out why my route was returning a 404 simply because my form was posting to something like /insertStudent when my route was defined as /admin/insertStudent

The third thing I suggest is to ensure the method you are calling exists. Are your methods really called postSignIn and postInsertStudent and not simply SignIn and InsertStudent? Keep in mind that both the URL and method names are case sensitive and should match where you define the route, the URL that is being called, and the method that is receiving the request.

Finally if all that checks out, one last thing I might suggest you try is running php artisan route:clear. This resets the route cache.

ChronoFish
  • 3,589
  • 5
  • 27
  • 38
  • i copy existing directory to new, also run `php artisan route:clear` but still page not found at new directory but on old directory page is found. any idea ? – questionasker Jun 28 '19 at 10:34
  • @anunixercoder, I suggest starting a new question being very specific about what you've tried and what error you're seeing. As mentioned, run php artisan route:list - you won't see what you want through the browser until route:list runs successfully with the expected output. – ChronoFish Jul 10 '19 at 18:53
  • 7
    Only other point to suggest is to ensure you are not using "$" in your routes. I tried using /users/edit_project_access/{$userId}/{$projectId} which also gave me a 404. Check that your URLs dont have $ signs as such: /users/edit_project_access/{userId}/{projectId} – Christoff X Nov 24 '20 at 12:41
  • 4
    May I suggest to add `check route ordering` ? That was what I did wrong – M at Mar 05 '21 at 07:39
  • 3
    Oh yes @ChristoffX that was it in my case. I had 'contacts/ads_off/{$contactId}'. Didn't see it before your comment. Thanks! – Urs Jan 01 '22 at 23:08
  • Another reason is: If you use Route::resource('xxx'.... then you can not use Route::get('xxx/test',... This leads to a 404 error. The solution here would be a different path. For Example: Route::get('test',... – Lehtis Feb 15 '22 at 08:52
  • On top of that, I have found it a good practice to optimize your app `php artisan optimize` – Ngugi Kiarie Apr 18 '22 at 08:39
  • In my case I had a mismatching `APP_URL` in the `.env`-File. – uruk Dec 06 '22 at 07:17
68

Please make sure you have Apache configured with the following information in /path/to/apache2/installation/conf/httpd.conf

<Directory "path/to/laravel/project/public">
    Allowoverride All
</Directory>

For the .htaccess file located in the public/ folder, make sure it includes the following:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
       Options -MultiViews
   </IfModule>
   Options +FollowSymlinks
   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>

Then enable the Apache mod_rewrite module:

sudo a2enmod rewrite
John Feminella
  • 303,634
  • 46
  • 339
  • 357
Ruberandinda Patience
  • 3,435
  • 3
  • 20
  • 18
  • Can you explain that further? I don't think that `/etc/apache2/apache2.conf` is present on Windows. Additionally, which parts are needed and why? Keep in mind that others should be able to learn from your answer – Nico Haase May 10 '19 at 14:43
  • ```sudo a2enmod rewrite``` fixed my issue thank you – IndexZero Jan 19 '23 at 01:34
52

I had the same Issue & resolved it by changing the way Routes were ordered.

Before:

Route::get('/p/{post}', 'PostsController@show');
Route::get('/p/create', 'PostsController@create');

After: (and that 404 disappeared)

Route::get('/p/create', 'PostsController@create');
Route::get('/p/{post}', 'PostsController@show');
Olly
  • 621
  • 6
  • 6
  • 4
    Taking the same example you have given, I think the reason is, If we had /p/{post} before, route is matching the request to /p/create to /p/{post} with create as the variable value to {post} . – Gayan Kavirathne May 15 '21 at 11:32
  • 3
    I find this especially true for resource routes. If you need to define an additional route to existing resource routes, you must declare it in your web.php BEFORE the resource routes, otherwise it will throw a 404 error for that route. – Grigoreas P. Sep 01 '21 at 10:13
  • This solved my problem but in my case: `Route::get('p/something', [PostController::class, 'something']);` should be ordered before `Route::resource('p', PostController::class);` – user16806454 Oct 17 '22 at 06:49
30

try

php artisan route:clear
php artisan route:cache

and then type this and see whether your route exists in the list

php artisan route:list

and also in Middleware\VerifyCsrfToken.php check post routes are allowed


class VerifyCsrfToken extends Middleware {

    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'api/*'
    ];

}

Ruhith Udakara
  • 2,236
  • 1
  • 18
  • 24
12

One more possibility... If the controller has Laravel's findOrFail() method, and the item sought is not found, a 404 page is returned.

  • I had this issue cause of the cookies, some old stuff in the cookies were being used the findOrFail causing a global 404 everywhere. – Steve Moretz Jul 18 '22 at 07:19
12

I fixed this error on Larvel 8.34 by doing these two steps

  1. php artisan route:clear
  2. Go to browser and (assuming your URL is localhost:8000) type localhost:8000/clear-cache. it should show "Done".

The issue should be fixed afterward.

Usama Imdad
  • 591
  • 5
  • 11
  • 1
    Dude I spent like an hour trying to figure it out what was going on! All new routes failed til ran the `route:clear` command, thanks! – Crysfel Dec 15 '21 at 16:33
8

This is old question but is active yet. for windows users if you using CAPITAL characters (upper-case letter) in your project folder you should using same in your browser. If your project is in "c:\xampp\htdocs\MyProject\" you should using MyProject as url instead of myproject for example in the above project (MyProject) following route:

Route::get('/dashboard', function () {
    return 'welcome to dashboard!';
});

will work if you use:

http://MyProject/dashboard

but is better using lowercase characters in directories

Hamid
  • 817
  • 1
  • 13
  • 29
7

I had the same issue and fixed by following

I have edit .htaccess file

RewriteEngine On
RewriteBase /path/of/project/folder/
# change above to your site i.e.,  RewriteBase /whatever/public/

Hope it will work for you

Ram Chander
  • 1,088
  • 2
  • 18
  • 36
  • It worked for me ! I had an alias and was working on subfolder of my localhost. Only homepage was working. After adding RewriteBase /myalias/ in htaccess file of public folder, my routes started working ! – Aurel May 26 '21 at 08:00
  • felling well, it was working for you too. :) – Ram Chander Jul 19 '22 at 17:24
4

Make sure you added correct Accept header.

In my case I tried to access api endpoints and each time recieved 404 resource not found. When I've added Accept: application/json header I got response from matched route

Anthony
  • 1,850
  • 2
  • 15
  • 15
4

Don't forget the order of your routes. For example, if you want to go to /path/second and have the following routes registered:

Route::get('/path/{dynamic_second}', 'Controller@actionDynamic');

Route::get('/path/{dynamic_second}/{dynamic_third}', 'Controller@third');

Route::get('/path/second', 'Controller@action');

This will break, as second is consumed by /path/{dynamic_second}.

If you want to access /path/second, you have to have your routes listed like so:

Route::get('/path/second', 'Controller@action');

Route::get('/path/{dynamic_second}', 'Controller@actionDynamic');

Route::get('/path/{dynamic_second}/{dynamic_third}', 'Controller@third');
Luke Madhanga
  • 6,871
  • 2
  • 43
  • 47
4

I was going crazy about my routes not working and here is also a quick fix. I had the following route:
users/{user}/user-detail-changes/{user-detail-change}/update-status
The fix was to rename parameters. Laravel doesn't accept minus character -. Rather than that you have to use camelCase or underscore (_) variable naming conventions.
So it has to be like:
users/{user}/user-detail-changes/{userDetailChange}/update-status

Franci
  • 81
  • 1
  • 5
3

When at the time of website hosting there is the problem 404 Not Found Error on requested pages. At That time there is only home page is work fine. And if i request another page for e.g. mydomain.com/any_page then it sends me to the 404 Not Found Error Then i looked at my controller files and other routes files to solve the problem. Finally i think there is absent of the .htaccess file When i just added the .htaccess file to the same folder of the index.php of the laravel project then it juts works file.

Conclusion

By adding the .htaccess file at the same folder location of the index.php it solves the 404 page not found error in my laravel Project.

Sandeep Kumar
  • 409
  • 4
  • 5
3

Make sure you are not running another laravel project on the same port.

Tonykay
  • 139
  • 7
2

make sure, the url passed is equal in your route. check the parameters and action in the form. To get clear answer post your mvc

Kayal
  • 23
  • 7
2

this can also happen if, for an API route, the Content-Type is not set to application/json when validation fails for a given request. This was the issue in my case.

Devqxz
  • 51
  • 1
  • 6
1

I had the same issue recently and I thought I was using a Reserved word, which I couldn't find in the list of reserved words of PHP.

Originally my Routes/web.php was:

Route::post('sysinfo/store',['as'=>'sysinfo.store', 'uses'=>'SysinfoController@store']);

After I changed to another name as shown bellow, it worked!

Route::post('newsysinfo/store',['as'=>'newsysinfo.store',   'uses'=>'SysinfoController@store']);

What's weird is that I have other working routes such as:

Route::get('sysinfo/',['as'=>'sysinfo.index',   'uses'=>'SysinfoController@index']);
Route::post('sysinfo/{sysinfo}',['as'=>'sysinfo.update',   'uses'=>'SysinfoController@update']);
Route::get('sysinfo/create',['as'=>'sysinfo.create',   'uses'=>'SysinfoController@create']);
Route::get('sysinfo/{id}/edit',['as'=>'sysinfo.edit',   'uses'=>'SysinfoController@edit']);
Route::get('sysinfo/{sysinfo}/destroy',['as'=>'sysinfo.destroy',   'uses'=>'SysinfoController@destroy']);

Although it's not an explanation of the causes of the problem, perhaps this workaround can help you.

Iago F.
  • 21
  • 3
1

As mentioned by webwizard02 in this GitHub comment:

Hello! I also encountered this problem before. Please check your Apache's httpd.conf file. Make sure that you enabled the LoadModule rewrite_module modules/mod_rewrite.so by removing the # from the start line of the code.

And also change inside the httpd.conf from Require all denied to Require all granted, AllowOverride All, and Options Indexes FollowSymLinks

Like This: Capture

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Hashmat Waziri
  • 476
  • 9
  • 9
1

For me, it was issue with .htaccess file. Make sure you have the .htaccess in public_html folder. I downloaded this file from one server but the dot(.) was removed by the downloader. Then when I uploaded in another server, the dot(.) was missing and I got 404.

My .htaccess looks like below:

# disable directory browsing, but specific file browsing is possible, e.g. /js/bootstrap.js
        
    Options ExecCGI Includes IncludesNOEXEC SymLinksIfOwnerMatch
#To set which file will be acted as index file: DirectoryIndex index.php
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>


<Files 403.shtml>
order allow,deny
allow from all
</Files>

# deny from some DDoS attackers ip
# deny from 69.171.251.0/24


# php -- BEGIN cPanel-generated handler, do not edit
# This domain inherits the “PHP” package.
# php -- END cPanel-generated handler, do not edit
Junaed
  • 1,457
  • 13
  • 15
1

On Centos

  1. Set the proper permission for the application files and directories.

    chown -R apache.apache /var/www/YOUR_LARAVEL_APP
    chmod -R 755 /var/www/YOUR_LARAVEL_APP
    chmod -R 755 /var/www/YOUR_LARAVEL_APP/storage
    
  2. SELinux enabled systems also run the below command to allow write on storage directory.

    chcon -R -t httpd_sys_rw_content_t /var/www/myLaravelApp/storage
    

Finally, access your Laravel application in a web browser.

Note: If you are using Nginx use nginx.nginx instead of apache.apache.

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
1

I tried almost all the solutions above but it didn't work. So I decided to change the route name to something else and it worked.

e.g: This was not working:

Route::get('product/show/{id}', [SiteController::class, 'showProduct'])->name('product.show');

This worked:

Route::get('raslimali/show/{id}', [SiteController::class, 'showProduct'])->name('product.show');
Simon Angatia
  • 688
  • 1
  • 10
  • 16
  • Habari Did you ever find out why it was not working the only change was product to raslimali. I think this was the issue --> One more possibility... If the controller has Laravel's findOrFail() method, and the item sought is not found, a 404 page is returned. – Emanuel Paul Mnzava Nov 27 '21 at 17:01
1

I am going to share my experience when it comes to this type of 404 error with an existing route.

In my case the problem was the length of the query, I had to increase the maxQueryString.

This post help me

Query string length: By default, Microsoft IIS accepts a maximum query string length of 2048 characters. If IIS receives a query string that is longer than 2048 characters, it will generate a 404.15: Query

String Too Long error URL length: Like the query string, each browser has a limit on the length of the URL it supports. For example, Internet Explorer accepts a maximum URL length of 2083. If the URL length exceeds the limit, you'll get a 404.14: URL too long error.

absolutkarlos
  • 570
  • 1
  • 9
  • 22
0

Changing the server port worked for me after trying several fixes all to no avail. I was running on loalhost:3000 and changed to localhost:4000. I think this might have to do with some memory cahe

0

Update 2020
There are many case happened above, I've once more case I met a long time but I still forget and sometimes I dont know why. This is some url patterns is the same, example abc.com/c/{get_an_id} and abc.com/c/get_text. You can change order but only first can run, 404 for the second url.

May'Habit
  • 1,334
  • 12
  • 18
  • This should be its own question. But the issue is the path with the parameter ({get_an_id}) needs to come second. Otherwise "get_text" would be sent as the parameter value. If it is already second and getting a 404, it's probably because a lookup is failing (.i.e abc.com/c/54 returns 404 because there is no id that matches 54 in your DB) – ChronoFish May 29 '20 at 11:44
0

Maybe this help someone, I fix this problem changing the order from my routes in laravel.

in my case i had the route ..users/crea --> 404 error

 Route::get('users', 'Admin\UserController@index')->name('users.index')
 Route::put('users/{user}', 'Admin\UserController@update')->name('users.update')
 **Route::get('users/crea', 'Admin\UserController@crea')->name('users.crea');**

and to fixed

 Route::get('users', 'Admin\UserController@index')->name('users.index')
 *Route::get('users/crea', 'Admin\UserController@crea')->name('users.crea');*
 Route::put('users/{user}', 'Admin\UserController@update')->name('users.update')
  
mFos
  • 11
  • 1
0

Im using Homestead, and after being stuck for quite some time trying everything that there was to try, what finally worked was re-checking my Hosts file (/etc/hosts on *nix systems), verifying my Homestead.yaml configuration file, and running the 'provision' command on the Homestead Vagrant Box.

The provision seems to have cleared some caches previously held or deployed the currently working site, honestly can't understand what changed since the databases were working and unit tests were passing.

But leaving it here in case it can help somebody out.

Lennin
  • 481
  • 5
  • 11
0

one thing i would like to add for user's who use nginx server and have configured server blocks(virtual hosts). check which server block is activated in your nginx conf files.

also in /etc/hosts file check whether you are using correct url to your server block or not.

also check your IP address there.

Plus all other point that are mentioned.

rajeshtva
  • 410
  • 6
  • 18
0

For me it was a permissions issue. User I was logged in with didnt have the necessary assigned role that gave the required permission to view said URL/section.

SupaMonkey
  • 876
  • 2
  • 9
  • 25
0

In my case, I was mistakenly overriding the crsf token on form submit and it was throwing a 404 instead of actually giving a more meaningful message.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
0

I had the same issue. The reason was that i was making https requests but i did not set-up https configuration in apache vhost. So apache was sending 404.

Abhinav Keshri
  • 595
  • 5
  • 20
0

In my case this was the final solution:

chmod -R 777 storage bootstrap/cache
ernesthm
  • 421
  • 4
  • 15
0

In the local environment, you have to change your port with the following command:

php artisan serve --port your_digit_port
0

In my case, I have a filename with extension as a parameter as you can see below:

Route::get('/attachment/{filename}', function() {
    //
});

I always get 404 error when I tried to access following URL from the browser:

http://localhost/attachment/filename.ext

After many hours of struggle and an advise from a friend, I finally found out that the filename.ext is treated differently in Laravel (as physical file), hence I need to change the filename parameter to either of one below:

  1. using base64encode(filename), or
  2. create parameter to split the filename into filename_only and extension (e.g. myfilename.txt into myfilename and txt)

To split the filename:

Route::get('/attachment/{filename_only}/extension/{extension}', function() {
    //
});

Hope this helps as an alternative answers.

Erwin
  • 71
  • 1
  • 9
0

In 2023 with Laravel 10 and Postman I had a similar problem. In my postman I had a variable base_url which ends with "/" so my base url was http://localhost:8000/ And in the request I had a line like this: {{base_url}}/api/some_endpoint

Because of two "/" symbols I had a 404 error saying that there is no api/some_route_name, but no mention about double "/" I spent about an hour to find that. Just keep in mind this case too

-1

dontforget, the url is sensitive case to the folder name

david valentino
  • 920
  • 11
  • 18
  • Can you explain further what this means? How did you guess that there is a problem using cases? How to solve this? – Nico Haase May 10 '19 at 14:42
  • 1
    if your htdocs subfolder contain capital case, then you must call the url with capital case.ie. folder is AvenGers inside the htdocs, then must call localhost/AvenGers – david valentino May 12 '19 at 03:37
-3

I had the same problem but the solution is that simple :

go to cmd and tap cd #your project path#

then tap php artisan serve

copy the link of the server given (http://127.0.0.1:8000) to your browser, and it'll work!

selharem
  • 74
  • 12