How to Remove PHP Extension With .Htacess File

Kevin Amayi Feb 02, 2024
  1. Remove PHP Extensions Using .htaccess File in PHP
  2. Remove PHP Extenstions Using .htaccess File With Conditional Statement in PHP
How to Remove PHP Extension With .Htacess File

We will look at the standard way to remove PHP extensions using the .htaccess file. You will also need to run this code from a server like Apache with PHP installed.

We will also remove PHP extensions using a .htaccess file with a conditional statement to prevent unnecessary looping when removing the PHP extension.

You will also need to run this code from a server like Apache with PHP installed.

Remove PHP Extensions Using .htaccess File in PHP

#remove php file extension-e.g. https://example.com/file.php will become https://example.com/file 
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Remove PHP Extenstions Using .htaccess File With Conditional Statement in PHP

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

Related Article - PHP File