← Older posts

Hide File Extensions in URL Using .htaccess Files

Posted on by Ray Pettersson

Recently, I’ve decided to bring an old project to life. It’s a social networking kind of blog called Design Made in Brazil, which among other things, is a showcase of design projects from when I was working in São Paulo, Brazil.

WordPress was the blogging management system of choice. However, with a big chunk of the website customized manually, I wanted to find a way to hide file extensions to maintain URL consistency across different enviroment. Besides that, it looks cleaner for the user, is easier to remember and increases the website findability.

With a bit of research, I’ve found that it’s possible to hide file extensions in URLs using .htaccess files. These files are very powerful and can do a bunch of other things, like:

  • Define default error documents;
  • Protect specific folders with passwords;
  • Enable SSI – Server Side Includes – access;
  • Block user access by IP, sites or referres;
  • Change default directory page;
  • Add MIME types;
  • Prevent hot linking of images or other files;
  • Prevent directory listing;

I’m not going to describe how to do all of that here. Nevertheless, I though it would be nice to add what other things it’s possible to do using .htaccess files. If you’re looking forward to do some of these other things I recommend you to visit

How to Hide File Extensions in URL Using .htaccess Files?

Create a file called “.htaccess”, include the piece of code below and upload the file to your site’s root directory. It looks simple, but some minor details can make this task seems a bit more complicated than it really is. Follow this four steps tutorial and you’re safe.

Keep in mind that some hosting companies do not allow use of .htaccess files. Depending on what they are doing, they can seriously slow down a server. I strongly recommend you to make sure you are allowed to use .htaccess before you actually use it.

Step 1. Open up a text editor and type the following code in it:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This code snippet is set to remove extension from PHP files, but you can change it to whatever type of file you're using. You can remove any extension be it .html, .asp, .cfm, or anything else, as long as they're all the same type.

Step 2. Save the file as "temp.htaccess.txt".

Make sure you use an ASCII text editor, like Windows Notepad or Mac TextEdit.

Step 3. Upload the file to your site's root directory

The .htaccess file must be uploaded as ASCII mode, not BINARY. You may need to change the file permissions to 644 or (RW-R--R--). This makes the file usable by the server, but prevents it from being read by a browser, which can seriously compromise your security.

Step 4. Using your FTP software, rename the file from "temp.htaccess.txt" to ".htaccess".

On Unix and unix-like operating systems, files that start with a dot are hidden files, so you may not be able to see the file after you rename it. To get around this, look for the "show hidden files" feature on your FTP software of choice.

Job done! The .htaccess file will change the way URLs are written. From now on instead of typing http://site.com/contact.php, you only need to enter http://site.com/contact to access the same page.

Posted in Tutorial | 1 Comment ← Older posts

Sponsors

Resources