Appearance
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:
- Navigate to your Laravel project directory in the terminal.
- Run the following command to start the built-in PHP web server:
console
php artisan serve- 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
- Open the URL in your web browser. The built-in server automatically serves from the
publicdirectory and routes all requests throughpublic/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:
- 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.
- 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>- Replace
example.comwith the domain name of your Laravel project and/path/to/laravelwith the actual path to your Laravel project on your server. TheAllowOverride Alldirective is required so that Laravel'spublic/.htaccessfile can handle URL routing correctly. - Restart Apache for the changes to take effect.
- 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;
}
}- Replace
example.comand/path/to/laravelwith your actual domain and project path. - Restart Nginx for the changes to take effect.
- 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.