How to remove php extension?

How to remove php extension

To remove the .php extension from a webpage for example your-website.com/about-us.php to your-website.com/about-us kindly add the following code inside the .htaccess file, but before making any changes in a .htaccess file its always recommended to take a backup for original .htaccess file.

cp –p .htaccess .htaccess-ori

It’s now safe to add following code in .htacccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

If you would like to add “trailing slash” end of the file name for example your-website.com/about-us/ instead of your-website.com/about-us , than simply refer following code instead of above code in .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Let us know, if you have any questions or doubts.

#KTCHost – A reliable hosting provider.

About Anant 376 Articles
Senior technical writer