Skip to content

How Can I Remove “public/index.php” in the URL Generated Laravel?

To remove "public/index.php" from the URL in a Laravel application, you can use the built-in PHP web server for development or set up a web server with Apache or Nginx. Here are the steps to remove "public/index.php" from the URL using the built-in PHP web server:

  1. Navigate to your Laravel project directory in the terminal.
  2. Run the following command to start the built-in PHP web server:

console
php artisan serve
  1. The built-in PHP web server will start and you will see the URL where the application is being served. The URL will be in the following format: http://localhost:8000
  2. Open the URL in your web browser. The built-in server automatically serves from the public directory and routes all requests through public/index.php, so you do not need to include it in the URL path.

If you want to use Apache or Nginx as your web server, you will need to set up a virtual host for your Laravel project. Here are the steps to set up a virtual host with Apache:

  1. Open the Apache configuration file in a text editor. The location of the configuration file may vary depending on your operating system and Apache installation.
  2. Add the following virtual host configuration to the Apache configuration file:
apache
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /path/to/laravel/public
    <Directory /path/to/laravel/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
  1. Replace example.com with the domain name of your Laravel project and /path/to/laravel with the actual path to your Laravel project on your server. The AllowOverride All directive is required so that Laravel's public/.htaccess file can handle URL routing correctly.
  2. Restart Apache for the changes to take effect.
  3. Open your domain name in a web browser and you will see that "public/index.php" has been removed from the URL.

For Nginx, add the following server block to your configuration:

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

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

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
  1. Replace example.com and /path/to/laravel with your actual domain and project path.
  2. Restart Nginx for the changes to take effect.
  3. Open your domain name in a web browser and you will see that "public/index.php" has been removed from the URL.

Note: After deploying to a production server, remember to update the APP_URL in your .env file to match your domain to ensure asset routing works correctly.

I hope this helps! Let me know if you have any questions.

Dual-run preview — compare with live Symfony routes.