Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

enexspecial's avatar

How can i solve this 404 not found error even when route exist

I have being trying to solve this issue about this 404 not found error i created a route and it keep giving me this error

0 likes
35 replies
Snapey's avatar

hmm let me see...... no, still not psychic

27 likes
TortleWortle's avatar

Is it in a route group with a prefix?

Is your route file cached?

Is it registered with the proper http method?

Does it show up when you do php artisan route:list?

1 like
enexspecial's avatar

Okay Here is my route: Route::post('/mappy','locationController@postLocation')->name('location');

Here is my controller: public function postLocation(Request $request) { $lat = $request->lat; $long = $request->lng;

    $map = Mappy::whereBetween('lat', [$lat-0.1,$lat+0.1])->whereBetween('long', [$long-0.1,$long+0.1])->get();

    return $map;
}

Here is my Js file: function searchInformation(lat,long) { $.post("{{URL::to('mappy')}}", {lat:lat, long:long}, function(match){ console.log(match); }); }

RamjithAp's avatar

Change your route to

Route::get('/mappy','locationController@postLocation')->name('location');

//Or if your posting some values at the time of page load try this

Route::any('/mappy','locationController@postLocation')->name('location');
enexspecial's avatar

I have changed it to Route::any('/mappy','locationController@postLocation')->name('location'); i still have the error

enexspecial's avatar

I did do the php artisan route:list and the route exist.

rumm.an's avatar

Seems like your AJAX is not hitting the correct route. Try this:

function searchInformation(lat,long) { 
    $.post(
          "{{ route('location') }}",  {lat:lat, long:long}, 
           function(match) { 
                console.log(match); 
           }
      );
 }

If this doesn't work, show your routes file and the result for php artisan route:list.

1 like
rumm.an's avatar

if you are sending AJAX in a seperate .js file, there {{ }} dont work because its javascript not Laravel Blade... So, you can try this:

function searchInformation(lat,long) { 
    $.post(
          "/mappy",  {lat:lat, long:long}, 
           function(match) { 
                console.log(match); 
           }
      );
 }
2 likes
Snapey's avatar

you are trying to send lat long with the request but the route is not expecting any parameters so it fails to match

enexspecial's avatar

@Snapey : I have manually entered the lat and long value into the database i want to retrieve the data from the database. Please what is the best way to go about it

@rumm.an : I have tried it and i am getting 500 internal server error and also i tried using api route and i am getting 401 (Unauthorized)

design_studio's avatar
  1. You said your route list results in: POST | mappy | location | App\Http\Cont rollers\locationController@postLocation | web,auth there is space in the middle of the word Cont rollers. I assume it is a mistake done when pasting the text here, but check it out.
  2. Are you running the command in the http://127.0.0.1:8000 or maybe in some sub-directory, for example http://127.0.0.1:8000/my_test_app/ ? if so @@rumm.an js points to the wrong url and it won't work, and you'll have to create the url in the js using laravel, like you tried.
1 like
rumm.an's avatar

see your route has auth middleware if you are making AJAX request internally, make sure you are sign in. And, if you get 500 Internal Server Error there might be something wrong within your Controller. Post your controller also.

raghumestry's avatar

Hi i suggest that place your route on the top of the all routes this is work for me

5 likes
mailnike's avatar

delete all files in the folder >> project folder >> bootstrap >> cache >> delete all files from this folder.

2 likes
naet's avatar

I had the same issue. I reset the .htaccess file and it works fine again.

osacaa's avatar

Please check for your APP_URL if it got more '/' than expected

kiloblaster's avatar

For those wandering for this, remember to enable the rewrite module with sudo a2enmod rewrite

1 like
sanikookalam's avatar

I have the same issue, in my case I realize that I have a form in a form and sometimes when the inner exist in the view, I click the submit button for the outer form, and it occurs 404 error I move the inner outside, and when they separated the problem has been solved.

tomasosho's avatar

Add this to .htaccess in your public folder

<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 ^(.*)/$ / [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>
tonystarks's avatar

I guess the problem is with the .htaccess file in the public folder, move it to the root directory

Snapey's avatar

@tonystarks whats with the irrelevant answer 4 years after the fact?

.htaccess belongs in the public folder and the public folder should be the document root

1 like
Ayyan's avatar

write (php artisan optimize) and restart your server

Christofer's avatar

If you are using Route::resource this can have undisclosed effects on other routes, don't know why.

Put your routes above any Route::resource

Laravel 10 - even though php artisan route:list shows a route, I was getting "404 not found". Only thing that fixed this was placing my route above the Route::resource for the URL sharing the same prefix.

1 like
chavole123's avatar

Run command line php artisan optimize:clear it's work fine for all..

1 like
gabriel9nc's avatar

If you have any subdirectory with same name of your route in public folder you get 404. Check this!

Next

Please or to participate in this conversation.