Today we will talk about how to remove the .php extension from www.example.com/file.php. We use the .htaccess file to remove the .php extension.
what is htaccess?
.htaccess file is used to manage URL redirects. For example, if we rename one of our Web pages by https://www.expertsphp.com/about to https://www.expertsphp.com/about-us, then whatever user is using this web page as its original Trying to view at URL, it will receive an HTTP 404 – Page Not Found Error.
While using URL redirect, we can redirect the old URL to the new URL. That means whatever will specify the Old URL in Visitor Web Browser, our .htaccess file will redirect it to the new URL again. Because of this, any of our visitors will not get the HTTP 404 – Page Not Found Error.
So let’s go and I’ll explain you with the code. To remove the .php file, write script code is written in the .htaccess file, which is the code I will provide you. This is a tested code. To apply this code first, you must create an .htaccess file and add this code written below to that htaccess file and add this file to your project folder. add it.
# Turn mod_rewrite on RewriteEngine On RewriteBase / ## hide .php extension # To externally redirect /dir/foo.php to /dir/foo RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R=302,L] # To internally forward /dir/foo to /dir/foo.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*?)/?$ $1.php [L]