How to Hide Extension of Page or File with .htaccess

This article is about How to Hide Extension of a File with .htaccess. You might have seen many URLs like http://example.com/about/. These URLs are made with .htaccess Rewrite Rules. In this way the URL becomes SEO Friendly (means search engine friendly URL), also the user can’t see the original file working behind this URL. These kind of URLs are easy to remember and increases returning visitor to your website.

Hide File Extension in URL with htaccess

We will be using .htaccess Rules for rewriting the URLS. Here is what your old Non-SEO Friendly URLs and new SEO Friendly URLs will look like:

Old URL with File Extension: http://example.com/about.php

New URL with Hidden Extension: http://example.com/about

Here is the code for Hiding Your File Extension.

Hiding a File Extension From URL with .htaccess:

This example will show how to hide your PHP File Extension through .htaccess:

Copy the above code, open your favorite Text Editor and paste the code in it, and then save the file with name: .htaccess. Now upload this newly created .htaccess file to the Root Directory of your website. i.e public_html directory.

If .htaccess file already exists in your website’s public_html directory, then you need to see the following code:

Find the line RewriteEngine on inside your existing htaccess file, and paste the above code after this line.

After applying above codes, you will become enable to open all your PHP files like this:

  • http://yoursite.com/about.php -> http://yoursite.com/about
  • http://yoursite.com/categories.php -> http://yoursite.com/categories
  • http://yoursite.com/examples/example1.php -> http://yoursite.com/examples/example1

If you want to Rewrite all your HTML files instead of PHP files, then replace the word php with html in the given htaccess code.

I will write another article in which i will cover the topic: Passing PHP file parameters by Rewriting URLs with .htaccess.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top