Skip to content

Laravel: Removing “public/index.php” from URL in Apache

Cleaner URLs in Laravel: Removing 'public/index.php. A Step-by-Step Guide to remove 'public/index.php' from URL in Laravel

Steps
3
Category
PHP
Covers
Apache, Laravel
On this page 3 steps

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 of 3Enable 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 of 3Configure 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:

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 of 3Restart 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.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

2 Comments

  1. 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?

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top