Desktop and Server Virtualisation Specialist 

Twitter LinkedIn E-mail
Precedence Technologies Ltd
Technology House, 36a Union Lane
Cambridge, CB4 1QB, United Kingdom
T: +44 (0)8456 446 800 / +44 (0)1223 359900

Using .htaccess files to map one file extension to another

At Precedence Internet, we use a webserver called Apache. This is most widely used webserver in the world. Apache is highly configurable and it is possible for some options to be set by the end user. This is performed by creating text file called .htaccess in your directories.

A brief guide is given below, but this is no substitute for having read the Apache documentation.

Rewriting one file extension to another (e.g. .html to .php)

If you initially designed your whole website using standard HTML pages, but then realised how fantastic PHP is as part of our Advanced web hosting package, this can leave you with a problem. HTML files tend to end in .html whereas PHP files end in .php. This means that if a visitor has bookmarked a page on your site the URL of which ends in .html then they will get a 404 file not found error message if this file is renamed to .php. You could set up a customised error message to alert the visitor, but this can be confusing for them.

Apache has a very powerful feature called mod_rewrite which allows you to dynamically rewrite URLs and filenames without the user knowing. Using mod_rewrite can be very complicated, but its use is discussed with examples at

We have chosen one example to describe here, namely file extension rewriting. The example given will map (for example), index.html to index.php if, and only if, index.php exists. Add the following lines to a .htaccess file (you may miss out the comment lines that begin with #):

    #   backward compatibility ruleset for rewriting document.html to document.php
    #   when and only when document.php exists but no longer document.html
(1) RewriteEngine on
(2) RewriteBase   /
    #   parse out basename, but remember the fact
(3) RewriteRule   ^(.*)\.html$              $1      [C,E=WasHTML:yes]
    #   rewrite to document.php if exists
(4) RewriteCond   %{REQUEST_FILENAME}.php -f
(5) RewriteRule   ^(.*)$ $1.php                   [S=1]
    #   else reverse the previous basename cutout
(6) RewriteCond   %{ENV:WasHTML}            ^yes$
(7) RewriteRule   ^(.*)$ $1.html
  1. Switch on rewriting
  2. Sets the base directory for the rewriting to happen in. This will need to be altered if you have files in subdirectories
  3. If the filename ends in .html, chop off the extension and keep a note of it by setting WasHTML to yes
  4. If a .php version exists then:
  5. Add .php onto the end of the filename to be used and skip the next rule
  6. Otherwise, if WasHTML (from 3) is set to yes, then:
  7. add .html to the end of the filename

If you want to rewrite different extensions (e.g. .htm to .html after moving from a Windows web server), then you will need to alter the highlighted sections above. The above example can be downloaded here.

© Copyright Precedence Technologies 1999-2024
Time elapsed:0.021