How to remove .php or .html extensions with .htaccess

In this post, I would like to explain HOW TO REMOVE .php OR .html EXTENSIONS FROM URL WITH HELP OF .htaccess.

Its not user and search engine friendly if .html or .php is in URLs, to make user and search engine friendly we have to remove the .html or .php extensions fron URLs by editing .htaccess file.

What is an .htaccess file

.htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance, if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.

In simple:An .htaccess file is a simple ASCII file that you create with a text editor like Notepad or TextMate. It provides a way to make configuration changes on a per-directory basis.


Please note that .htaccess is the file’s extension. It isn’t file.htaccess, it is simply .htaccess.

.htaccess files affect the directory in which they are placed and all sub-directories. For example, if there is one .htaccess file located in your root directory of yoursite.com, it would affect yoursite.com/article/, yoursite.com/article/images/, etc.
It is important to remember that this can be prevented — for example, if you don’t want certain .htaccess commands to affect a specific directory — by placing a new .htaccess file within the directory you don’t want to be affected by the changes, and removing the specific command(s) from the new .htaccess file that you do not want to affect this directory.

What can we do with .htaccess file

  • Rewrite URIs
  • Redirect the user to different page
  • Password protect a specific directory
  • Block users by IP
  • Preventing hotlinking of your images
  • Specify your own Error Documents

that’s enough about .htaccess file, let’s see how to remove extensions from URLs.

Removing .html Extension

For example we want to remove .html from yoursite.com/about.html to yoursite.com/about
write these code in your .htaccess file.

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

Removing .php Extension

For example we want to remove .html from yoursite.com/about.php to yoursite.com/about
write these code in your .htaccess file.

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

That’s all! You can now link pages inside the HTML document without needing to add the extension of the page. For example:

<a title="about us" href="http://yoursite.com/about">About Us</a>