Removing .php extension from URLs using .htaccess?

Rahul Khosla picture Rahul Khosla · Apr 14, 2013 · Viewed 8.9k times · Source

Is it possible to remove the .php part from all of my URLs using a .htaccess file?

Two examples:

http://url.com/home.php
http://url.com/shops.php

Become:

http://url.com/home/
http://url.com/shops/

All the help would be massively appreciated.

Answer

Rick Kuipers picture Rick Kuipers · Apr 14, 2013

This should work:

RewriteEngine On
RewriteRule \/([^\/]+)\/$ $1.php

It will serve you the file named after the last segment of the url:

http://example.com/shops/shopname1/ -> shopname1.php

http://example.com/shops/ -> shops.php