How to remove .php extension from URL using htaccess
Remove the .php extension from the URL
To remove the .php
extension from a PHP file for example phpgurukul.com/contactus.php
to phpgurukul.com/contactus
you have to add the following code inside the .htaccess
file:
1 2 3 |
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php [NC,L] |
In the above code, the first line checks that the request is a URL and not a directory. The second line redirects matching URLs with .php extension to those without the file extension. The last line redirects matching requests to URLs without a PHP file extension.
Similarly, we can remove the .html extension from the URL
1 2 3 |
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html [NC,L] |