How to Remove file extension from url using .htaccess

Remove file extension from any file whether it’s .php, or .html. you can achieve this using .htaccess file. suppose you have website called example.com/home.php and if you want to remove extension then you should use it. main benefit is create better optimized SEO structure and security perspectives. I’ve give an example of apache server.

.htaccess

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

RewriteRule ^(.*)$ $1.php [NC,L] this line remove the extension.
RewriteCond %{REQUEST_FILENAME} !-d this will check if request is URL not directory
RewriteCond %{REQUEST_FILENAME}.php -f This will check the file without the extension

You can do similar with .html as well. this will allow the html file to run without extension.

Remove file extension on .html file

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html [NC,L]

Save .htaccess file and test it on browser window .html and .php extension are gone and you seen user friendly URL on browser.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply