In order to make the URLs more user and search engine friendly. You can remove the .php extension from a PHP page and .html or .htm extension from HTML pages? In this tutorial I’ll show you how to do that easily, by editing the .htaccess file.

What is an .htaccess file

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.

.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, it would affect,, 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 affected with the changes, and removing the specific command(s) from the new .htaccess file that you do not want affecting this directory.


With an .htaccess file you can:

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

In this tutorial we’ll be focusing only on rewriting URIs.

Removing Extensions

To remove the .php extension from a PHP file for example to you have to add the following code inside the .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

If you want to remove the .html extension from a html file for example to you simply have to alter the last line from the code above to match the filename:

RewriteRule ^([^\.]+)$ $1.html [NC,L]

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

<a href="" title="wallpaper">wallpaper</a>

Adding a trailing slash at the end

If you want to add a trailing slash at the end. Ignore the first snippet and insert the following code. The first four lines deal with the removal of the extension and the following, with the addition of the trailing slash and redirecting. Link to the HTML or PHP file as shown above. Don’t forget to change the code if you want it applied to an HTML file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Some people asked how you can remove the extension from both HTML and PHP files. I haven’t got a coding solution for that. But, you could just change the extension of your HTML file from .html or .htm to .php and add the code for removing the .php extension.


Attention GoDaddy users: In order to remove the extensions you need to enable MultiViews before. The code should look like this:

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

If you’re worried that search engines might index these pages as duplicate content, add a canonical meta tag in your HTML head:

<link rel="canonical" href="">

What is a Canonical URL?

Everyone knows Google penalizes websites for having “duplicate content.” So, what happens when you have two URLs that point to the same page?
For example, and might go to the exact same product page. Since two URLs are being used and the page content is the same for both of them, Google thinks you have two pages with duplicate content.

Canonical URLs are used to prevent this. When Google crawls the page, it will see the Canonical URL and this is what Google recognizes as the “official” URL of the page. It will use this URL to index the site instead of the one used to access the page.


For those who are not so experienced with .htaccess files there is an online tool for creating .htaccess files. It is pretty good for novice users and very easy to use. Visit their website.