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

Updated: Mar 04, 2025

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

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

  1. Update the .htaccess file:

Open the .htaccess file located in the public directory of your Laravel project using a text editor. Add the following rules at the beginning of the file:

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

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

These rules will remove the need for the .php extension for all requests and will redirect non-existent files to the index.php file.

  1. Update the web.php file:

Open the web.php file located in the routes/ directory of your Laravel project using a text editor. Add the following line at the beginning of the file:

Route::pattern('controller/{controller:[a-zA-Z0-9]+}', '[a-zA-Z0-9]+');
Route::pattern('route/{route:[/a-zA-Z0-9_-]+}', '[a-zA-Z0-9_-]+');

These lines define custom route patterns that will allow Laravel to match routes without the .php extension.

  1. Test the configuration:

Restart your Apache or Nginx server and test the configuration by visiting your Laravel application's URL without the .php extension. For example, if your Laravel application is accessible at http://example.com/public, try visiting http://example.com/home instead of http://example.com/public/home.php. If everything is set up correctly, you should see the HomeController's content displayed in your browser.