The capabilities of .htaccess are incredible. Among some of the goodies are protecting directories (even with a username and password), filter bot traffic, block IPs, and my newest favorite… masking files!

I’ve had the pleasure of upgrading some old HTML files into a CMS with dynamic content. No problem! But what about all the search engine traffic that is still going to the old HTML pages? That stuff is gold and we don’t want to mess with redirecting it.

Only 4 lines in my .htaccess file is all I need!

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)\.htm loadpage.php?$1

The code above can be explained as follows:

  • RewriteEngine On
    This tells the web server to enable the mod_rewrite engine.
  • RewriteCond %{REQUEST_FILENAME} !-f
    If an actual file exists with the name we are trying to mask, don’t mask it and proceed normally.
  • RewriteCond %{REQUEST_FILENAME} !-d
    If an actual directory exists with the name we are trying to mask, don’t make it and proceed normally.
  • RewriteRule (.*)\.htm loadpage.php?$1
    If the file you are masking ends in .htm*, then everything before the .htm* is stored in the $1 variable which is used as the QUERY STRING in our loadpage.php script.

I am trying to reach the page at
My htaccess will check to see if a file or directory have the name “contact.html”. Assuming not, everything before the .htm* in “contact.html” will be stored in the $1 variable. So now our loadpage.php will be executed with the QUERY STRING of “contact”.

Our loadpage.php can now use the QUERY STRING as a “slug” or keyword to access your database to know what page to load.