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

daveb2's avatar
Level 3

Laravel routes not recognised

Hi all,

I have an infuriating issue here which crops up every now and again.

Sometimes on a project when I go to add a new route, it will not be recognised and it just keeps returning a 404.

In this particular instance I have a new V11 project with a very basic web routes file (I haven't added much yet):

web.php

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
    return redirect('https://www.google.com.au');// test redirect
});

Route::get('status', function () {// for DO load balancer checks
    return response('OK');
});

Route::get('status2', function () {// for DO load balancer checks
	return response('OK');
});

The /status route works, so I created a copy of that route which I called 'status2'.

I ran a php artisan optimize:clear successfully. I run php artisan route:list and I see the new route:

  GET|HEAD  status .........
  GET|HEAD  status2 ..........

But when I hit /status2 in a browser I get a 404 error. However, I can access /status fine. What gives??? This catches me out quite often, and I waste so much time trying to sort it out.

I am hosting on a local VM running ubuntu, with nginx and SSL with a self-signed certificate for testing. Hostname resolution is via local hosts file. I've reloaded and restarted nginx. I've cleared every cache I can think of, tried different browsers, etc. but all return 404.

0 likes
6 replies
LaryAI's avatar
Level 58

When encountering a 404 error for a route that appears to be correctly defined in Laravel, there are several potential causes and solutions to consider:

  1. Check Nginx Configuration: Ensure that your Nginx configuration is correctly set up to handle Laravel routes. The try_files directive should be properly configured. Here is a typical configuration snippet for Laravel:

    server {
        listen 80;
        server_name yourdomain.test;
        root /path/to/your/laravel/public;
    
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust PHP version as needed
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    
  2. Check SSL Configuration: If you are using SSL, ensure that your SSL configuration is correct and that the server block for HTTPS is properly set up.

  3. Clear Browser Cache: Sometimes, the browser cache might cause issues. Try clearing your browser cache or accessing the route in incognito mode.

  4. Check Hosts File: Since you mentioned using a local hosts file for hostname resolution, double-check that the entry is correct and points to the right IP address.

  5. Check for Typos: Ensure there are no typos in the route definition or when accessing the route in the browser.

  6. Check Middleware: Verify that there are no middleware restrictions that might be affecting the new route. You can check this in your app/Http/Kernel.php file or any middleware applied to the route group.

  7. Check Route Caching: Although you mentioned running php artisan optimize:clear, ensure that route caching is not causing issues. You can disable route caching during development by avoiding php artisan route:cache.

  8. Check for Conflicting Routes: Ensure there are no other routes that might conflict with /status2.

If none of these solutions resolve the issue, consider checking the Nginx error logs for any additional clues. You can usually find these logs in /var/log/nginx/error.log.

daveb2's avatar
Level 3

@LaryAI Thanks Lary, but these are just the common first suggestions. Since the site is responding at all that rules out #1-5. I haven't added any middleware so that rules out #6. I don't use route caching and I have run php artisan optimize:clear so that rules out #7. There are no other routes (new project new route) so that rules out #8. And the error is a 404 error so it is not appearing in any logs. Hence the 'frustrating' nature of this error.

jlrdw's avatar

@daveb2 did you run route:clear

I setup same routes, all working.

1 like
Tray2's avatar

@daveb2 Sounds to me like you run some caching/optimize artisan command, and you should never do that in dev, save that for staging and production.

daveb2's avatar
Level 3

@Tray2 sorry I'm not sure what you mean. IMO there are too many optimize and clear commands for me to keep track of (I must be too dumb). I rely on optimize:clear to clear everything - config, events, routes, views - like it says. If that is not enough then I don't know what to do here as everyone has a different way to do this!!

daveb2's avatar
Level 3

Well, I think the issue was confused for me by the caching stuff, but ultimately I think it was caused by similarly-configured servers and multiple hosts file entries uncommented. I solved the problem by commenting out my live server instance in the hosts file. Thanks everyone.

Please or to participate in this conversation.