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

enadabuzaid's avatar

I have problem in api

I am using laravel 11 and the localhost/api/cars is working but when deploying it on the server via forge {website usr }/api/cars => give me 404 not found

0 likes
5 replies
gych's avatar

Is it the first time you've deployed to the server and can you share your deploy script?

enadabuzaid's avatar

@gych I fixed it thank you ... just install api on the server because I am using laravel 11 php artisan instal:api

1 like
LaryAI's avatar
Level 58

It sounds like you're encountering a common issue when deploying a Laravel application to a production server. Here are a few steps you can take to troubleshoot and resolve the 404 error for your /api/cars endpoint:

  1. Check your route definition: Ensure that the route is correctly defined in your routes/api.php file.

  2. Verify .htaccess file: Make sure that the .htaccess file is correctly set up in your public directory. This file is responsible for rewriting URLs so that they are handled by Laravel's index.php file.

  3. Confirm mod_rewrite is enabled: If you're using Apache as your web server, ensure that the mod_rewrite module is enabled. This module is necessary for the .htaccess file to work properly.

  4. Check your web server configuration: If you're using Nginx, make sure that you have the correct configuration to handle requests and pass them to Laravel's index.php file.

  5. Clear route cache: After deployment, you may need to clear your route cache. You can do this by running the following command via SSH on your server:

php artisan route:cache
  1. Check storage permissions: Ensure that the storage and bootstrap/cache directories are writable by the web server. Incorrect permissions can cause unexpected behavior.

  2. Inspect environment configuration: Make sure that your .env file on the server has the correct settings for your production environment.

  3. Review server logs: Check the server error logs for any specific messages that might indicate what's going wrong.

  4. Ensure API prefix: Verify that the api middleware group is correctly set up in your app/Providers/RouteServiceProvider.php file, which automatically prefixes your API routes with /api.

Here's a basic example of what your Nginx configuration might look like:

server {
    listen 80;
    server_name example.com;
    root /path/to/laravel/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php index.html index.htm;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Remember to replace example.com with your domain and /path/to/laravel/public with the actual path to your Laravel application's public directory.

If you've gone through all these steps and are still facing issues, it might be helpful to provide more specific details about your server configuration and any error messages you're seeing in the logs.

Please or to participate in this conversation.