How to Remove .php, .html Extensions using .htaccess
In this tutorial, I am going to show you How to Remove Extensions from the filename using .htaccess.Many client requirements want to remove .html or .php extensions to make URLs more SEO-friendly. It is very easy to remove the file extension.
Table of Contents
- Removing .php Extension
- Removing .html Extension
- Conclusion
Removing .php Extension
The remove .php extension from a PHP file, So you have to add the given below script in .htaccess file.
#.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Now you can use link pages inside HTML without extension.
<a href="http://devnote.in/laravel">Laravel</a>
Now when the user will access /laravel it will show the content of the laravel.php file.
Note: if someone user will access using /laravel.php, then it will not redirect to /laravel
So you make redirection you have to add some more code in your .htaccess file. So you can put below code in .htacess file.
#.htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Removing .html Extension
The remove .html extension, Only change .php extension to .html. then simply add the given below code in your .htaccess file.
#.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Now when the user will access /laravel it will show the content of laravel.html file.
Note: if someone user will access using /laravel.html, then it will not redirect to /laravel
So you make redirection you have to add some more code in your .htaccess file. So you can put below code in .htacess file.
#.htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Conclusion
In this tutorial you have learned how to remove .php, .html file extension from URL using .htaccess file.