Laravel routes return a 404 error when the URL you visit does not match any defined route, or when the matched route is blocked by server configuration, caching, or namespace issues. Fixing it usually involves clearing route caches, checking the web server rewrite rules, and verifying the route file and controller namespace.
Background: How Laravel Routing Works
Laravel uses a front controller pattern. Every request is sent to public/index.php, which boots the application and passes the request to the router. The router compares the request method (GET, POST, etc.) and URI against all routes defined in routes/web.php and routes/api.php. If a match is found, the route’s controller or closure runs; if not, Laravel throws a NotFoundHttpException, which renders as a 404 page.
This means a 404 is not always a bug in your route definitions. It can be caused by the web server never sending the request to Laravel in the first place, by a cached route list that is outdated, or by a mismatch between the URL you type and the route pattern.
Common Cause #1: Missing or Incorrect Rewrite Rules
On Apache and Nginx, the server must be configured to route all non-existent files and directories to public/index.php. If this rewrite rule is missing or misconfigured, requests to routes like /about will be treated as requests for a real file, and the server returns a 404 before Laravel ever runs.
For Apache, the .htaccess file in the public directory should contain:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
For Nginx, the server block should include:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
If you are using Laravel Sail or Valet, these rules are already set up. On shared hosting, you may need to upload the .htaccess file manually or enable mod_rewrite in your hosting panel.
Common Cause #2: Route Cache Is Outdated
Laravel can cache your routes to speed up the application. If you add, remove, or change a route after running php artisan route:cache, the cached file still contains the old route list. This is a frequent cause of 404s on production servers after deployment.
To fix it, clear the route cache:
php artisan route:clear
Then, if you want to re-cache, run:
php artisan route:cache
Also clear the config and view caches, because they can sometimes interfere:
php artisan config:clear php artisan view:clear
If you do not have command-line access, delete the cached files in bootstrap/cache/ (specifically routes-v7.php or similar) via FTP or your hosting file manager.
Common Cause #3: Wrong Route File or Namespace
Routes are loaded from routes/web.php by default. If you put a route in routes/api.php but try to access it without the /api prefix, you will get a 404. Similarly, if you define a route with a controller reference like UserController::class and the namespace is wrong, Laravel will throw a 404 (or a class-not-found error) when you visit the URL.
Check the RouteServiceProvider in app/Providers/RouteServiceProvider.php. It defines the namespace for controllers in each route file. If you moved a controller to a subfolder, update the use statement in the route file or the namespace in the provider.
Also verify that the route file itself is being loaded. If you created a new route file and did not register it in RouteServiceProvider, Laravel will not see those routes.
Common Cause #4: URL Trailing Slash or Case Sensitivity
Laravel routes are case-sensitive and exact-match by default. A route defined as /about will not match /About or /about/ (unless you add a trailing slash pattern). If a user manually types a URL with a trailing slash, they may get a 404.
To make a route accept an optional trailing slash, you can define it with a regular expression:
Route::get('about/{?}', function () {
return 'About page';
})->where('?', '.*');
But this is rarely needed. Instead, ensure your links use the correct URLs. If you are using route() helper in Blade, it will generate the exact URL.
How to Debug a 404 Step by Step
When you get a 404, follow this sequence to isolate the cause:
- Run
php artisan route:listto see all registered routes. Check if your URL appears in the list. - If the route is missing, check the route file and the
RouteServiceProvider. - If the route exists, test the URL directly in the browser. If it works, the problem is likely caching or server config.
- Clear the route, config, and view caches.
- Check the server error log. If you see a rewrite error, fix the
.htaccessor Nginx config. - Temporarily rename
public/index.phpto something else and access it directly to confirm PHP is running. - If you are using Laravel Homestead or Valet, restart the service.
Comparing 404 vs 500 Errors
A 404 means the route was not found. A 500 error means the route was found but something went wrong during execution (e.g., a missing controller method, a database error, or a syntax error). If you see a 500, the routing is working; the issue is in your controller or view. If you see a 404, the routing itself is failing.
This distinction is important because the fixes are completely different. For a 500, you would check logs, enable debug mode, and inspect the controller. For a 404, you focus on the route definitions and server rewrite rules.
Prevention and Best Practices
To avoid 404s in the future:
- Always run
php artisan route:clearafter changing routes on production. - Use named routes and the
route()helper instead of hardcoding URLs. - Keep your
.htaccessor Nginx config in version control. - Test new routes locally before deploying.
- If you use a subdirectory installation, ensure the
APP_URLin.envmatches the actual domain.
Frequently Asked Questions
Why does my Laravel route work on localhost but not on the server?
Usually this is a server configuration issue. The most common causes are missing rewrite rules on the production server, an outdated route cache, or the public directory not being set as the document root. Check your Nginx or Apache config and clear the route cache after deployment.
Can a 404 be caused by a missing controller?
No. If the controller class is missing, Laravel will throw a 500 error (or a class-not-found exception), not a 404. A 404 specifically means the URL does not match any route. If you see a 404, the route itself is not being found.
How do I fix a 404 for a route that exists in the route list?
If php artisan route:list shows the route, then the route is registered. The problem is likely the server rewrite rules or the URL you are using. Check the exact URI in the route list, including any prefixes, and ensure your web server is sending requests to index.php.
Does Laravel cache routes automatically?
No. You have to run php artisan route:cache manually. However, some deployment tools (like Envoy or Forge) may run it automatically. If you are using a shared host, the cache might persist from a previous deployment. Always clear it after updating routes.
Why does my API route return 404 when I access it without the /api prefix?
Routes in routes/api.php are automatically prefixed with /api by the RouteServiceProvider. If you want to access them without the prefix, you need to either move the route to routes/web.php or modify the provider to remove the prefix. The default behavior is intentional.
What should I do if clearing the cache and fixing rewrite rules still gives a 404?
Check the APP_URL in your .env file. If it is set to a different domain than the one you are using, Laravel may generate URLs that do not match. Also verify that the public folder is the document root. If you are using a subdirectory, you may need to adjust the server config to point to the subdirectory’s public folder.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- DisplayPort vs HDMI: Which Cable Should You Use?
- Soundbar vs 2.1 Speaker System: Which Is Better?
- Mouse Moving on Its Own? Causes & Fixes
- Slack Channels for Construction Projects: Setup Guide
- Surface Won’t Turn On: Quick Fixes1.8 h