Remove .PHP Extension from URL using .htaccess

We do see many PHP URLs without .php extension this can easily be achieved simply adding the following in .htaccess file.

Note: .htaccess file must be placed in the directory where PHP files are placed.

.htaccess code

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#RewriteRule ^(.*)$ $1.php #enable it <= if / is not required after URL
RewriteRule ^(.*)/$ $1.php [L] #disable it <= if / is not required after URL
</IfModule>

After this create a file of PHP in the same folder then you can access that URL without .php extension. I have also placed slash in the end of URL if you don’t want it so simply disable last time by # comment and enable second last line by removing # before it in the above htaccess code.

Example URL with Slash:

http://localhost/tutorials/Extensionless/newpage/

Example URL without Slash:

http://localhost/tutorials/Extensionless/newpage

Leave your comments if you find this tutorial helpful.