Modify the Laravel Her configuration to automatically resolve requests without the .php extension.

Updated: Mar 04, 2025

Modify the Laravel Her configuration to automatically resolve requests without the .php extension.

To modify the Laravel configuration to automatically resolve requests without the .php extension, you'll need to update the .htaccess file in your public directory and the web.php file in your Laravel project. Here are the steps to follow:

  1. Update .htaccess file:

First, you need to configure your Apache server to remove the .php extension from the URLs. Open the .htaccess file in your public directory and add the following rules:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php [QSA,L]

This rule will check if the requested file exists in the public directory. If it does, the request will be served directly. If not, it will be passed to Laravel's index.php file for processing.

  1. Update web.php file:

Next, you need to update the web.php file in your Laravel project to remove the .php extension from the routes. Open the web.php file and add the following line at the beginning:

Route::pattern('controller/{controller:[A-Za-z0-9_-]+}', '[A-Za-z0-9_-]+');

This line defines a custom route pattern that allows you to use lowercase controller names without the .php extension.

Now, you can define your routes without the .php extension. For example, instead of:

Route::get('/home.php', 'HomeController@index');

You can use:

Route::get('/home', 'HomeController@index');

With these changes, Laravel will automatically resolve requests without the .php extension.