How to Remove .php, .html Extensions with .htaccess File

hello friends, in today’s tutorial you will learn how to remove .php and .html file extensions from URLs using .htaccess file, you might have seen that some sites did not displays .php or .html extension even they are created in core, normal php, you can also do the same for your site or projects using .htaccess file, recently i was working on my project and i wanted to remove the extensions from my website, in order to make the URLs more user friendly. so i was thinking why not to share this simple .htaccess tip with you, well this was first .htaccess post on my blog and i will post some more tips and tutorials on this blog stay tuned, let’s have a look.

How to Remove .php, .html Extensions with .htaccess File

Let’s Start

read here official .htaccess
An .htaccess file is a simple ASCII file that you can create with a text editor like Notepad or Notepad++ . It provides a way to make configuration changes on a directory/folder. instead of this we can do more thing with .htaccess file like Redirect the user to different page, Password protect a specific directory, Rewrite URI etc. we will see it next time.

remove .php extension

In order to remove .php extension from your site url, for example suppose you have following url
http://yoursite.com/signup.php
and you want url like this
http://yoursite.com/signup
so which one is better, of course the second, now to achieve this you have to put following code inside .htaccess file so create a new file using any text editor and save it as (.htaccess) but one thing you should keep in mind that the .htaccess is the file’s extension, following code is for .php extension.


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

after it create a new file as login.php and save it in your root folder with where you have saved .htaccess file, then you can access that URL without .php extension, try it.


<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Remove .php extension with .htaccess by https://codingcage.com/</title>
         
        </style>
    </head>
<body>
 
<h1>This is login.php page | <a href="login">click here</a> to remove .php extension and see the url in addressbar</h1>

<a href="signup">signup here</a>
 
</body>
</html>

now you have a url like this : http://www.yoursite.com/login

remove .html extension

do the same as i showed above but you simply have to alter the last line from the code above to match the filename:


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html-f
RewriteRule ^(.*)$ $1.html
</IfModule>

Adding a trailing slash at the end

ok we have done it, now how about adding trailing slash at the end of the url like this.

http://yoursite.com/login/

if you want this just replace “RewriteRule ^(.*)$ $1.html” code with this “RewriteRule ^([a-z]+)\/?$ $1.html [NC]” and you are done and you have url like this :

http://yoursite.com/login/

final .htaccess code to hide .php , .html


<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
#RewriteRule ^([a-z]+)\/?$ $1.php [NC]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
#RewriteRule ^([a-z]+)\/?$ $1.html [NC]

</IfModule>

You can now link your html and php pages inside the HTML document without .php, .html extension of the page. For example:

<a href="http://yoursite.com/login">login</a>

That’s it! hope you guys like this post and please don’t forget to share.