Use .htaccess to hide file extensions

Create a file at the root of your web server called “.htaccess”. Put the following code inside:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php

This will make it so you can access all your PHP pages without having to type the “.php” extension the URL or your <a href>s. i.e., you can access “yoursite.com/about.php” just by going to “yoursite.com/about”. The actual file names should still have the .php extension though. You can do this with whatever file type you like too.

8 thoughts on “Use .htaccess to hide file extensions

  1. Code is exactly the same as in my post, except you need to replace both instances of “php” with “html”. You also must be running Apache on your server with mod_rewrite enabled/installed.

  2. i used the code above and i can now go to http://www.adiamondclean.co.uk/index and it works the same as with the .html at the end but when i click on my page tabs at the top i.e. contact us it goes to the full url with /contact.html at the end so why is this how do i stop this from happening without having to go into each page and take off the html extension in the code? is there a code that works in the .htaccess file to make this work properly?

    thanks [email protected]

  3. @steven: You really should go into each page and fix the extensions, but I believe you can redirect them by adding another rule,

    RewriteRule (.*)\.html $1 [R=301,NC]

  4. @Sunil: No. You need to provide more detail. I suggest you ask on stackoverflow.com.

Leave a Reply

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