When you create a new Laravel project, the default configuration requires that you include “public/index.php” in the URL to access your application. This can be cumbersome and can make your URLs look clunky and unprofessional. Fortunately, there is a simple solution to this problem, and in this tutorial, we will show you how to get rid of “public/index.php” in Laravel URLs.
Step 1: Enable Apache’s mod_rewrite Module
The first step in removing “public/index.php” from your Laravel URLs is to enable Apache’s mod_rewrite module. This module allows you to manipulate URLs in Apache, and Laravel uses it to create cleaner URLs. You can enable mod_rewrite by running the following command:
sudo a2enmod rewrite
Step 2: Configure Your .htaccess File
The next step is to configure your .htaccess file. The .htaccess file is used by Apache to specify configuration settings for specific directories. In this case, we will use it to tell Apache to remove “public/index.php” from Laravel URLs. Before creating file, make sure that you have enabled .htaccess in Apache virtual host of the Laravel application.
To create or edit the .htaccess file, run the following command:
sudo nano /var/www/html/.htaccess
In the file, add the following code:
1 2 3 4 | <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule> |
This code tells Apache to redirect all incoming requests to the “public” folder, where the Laravel application resides. As a result, the “public/index.php” part of the URL will be removed.
Step 3: Restart Apache
The final step is to restart Apache for the changes to take effect. You can do this by running the following command:
sudo systemctl restart apache2
Conclusion
In this tutorial, we have shown you how to get rid of “public/index.php” in Laravel URLs. By enabling Apache’s mod_rewrite module and configuring the .htaccess file, you can create cleaner and more professional-looking URLs for your Laravel applications. With this simple solution, you can enhance the user experience of your application and make it more accessible and user-friendly.
2 Comments
Rahul, this probably works for the ‘/’ route. I’m in trouble with the ‘/mypage’ route. To let it work I have to use ‘/index.php/mypage’ witch is not so nice. Can you teel me how to get rid of index.php for this kind of route?
not work in local server like xampp