How to Map Custom Server File Extensions
Server administration is a rarely discussed topic here on Build Internet, but today’s tutorial is a quick and easy modification that may prove useful to some. At the very least, it will save you from a “How did they do this?!” Google search someday.
This post also comes with a preface. While this may be a neat trick, it’s not something that should be done without a good reason. This could very well be a case of decorating instead of designing. With that in mind, let’s learn something to impress your (tech savvy) friends.
A Custom Example in Naming
Take a minute to load the URL below in a new window. The page content is irrelevant. What’s important is that the page is a custom (.OMR) extension running the PHP MIME type. Bonus points for anyone who got the acronym.
What’s going on? I’ve edited our server’s configuration file to handle the new OMR type as a PHP file. I’ve also set up the OMR extension to take priority over all other file types when loading a default index page. This process is remarkably simple.
This tutorial is intended for servers running Apache configurations. Statistically, this will include most of you.
Reinventing the File Type
File extension names are little more than letter combinations. Their real meaning comes from generally accepted standards. A file with JPG extension is expected to be an image, and programs will default accordingly. These are the. But these definitions can be changed.
Think of it like a vanity license plate for files. Certain sites, like the financial management site Mint, use custom extensions (e.g. index.mint) as an added layer of security on their pages. By obscuring the coding languages and systems used, they are adding one extra step to getting through server security. The majority of us aren’t running bank-level operations though.
Online file formats are specified using something called MIME types. A file’s MIME type controls how it is to be handled by servers, clients, etc. Many types come predefined (e.g. JPG, HTML), but we’re also able to add in new ones. In the same way that both HTM and HTML represent the same MIME type, we can tell the server to treat a custom file extension like an existing definition. A file extension of XYZ could then become another name for PHP documents.
Declare a New File Type
The code for this is surprisingly short and sweet. Keep in mind that the # symbol represents a commented line, and is not actually part of the required code. This block can be copied directly into either of the two methods outlined in the next section, and does not require any special prefacing to work.
# Add extension OMR to PHP type definition
AddType application/x-httpd-php .omr
# Sets index.omr into default page stack
DirectoryIndex index.omr index.php index.htm index.html
Putting it in Action
You can put append this code to either of the files outlined below. I recommend setting up an isolated directory locally using MAMP/WAMP in order to keep from potentially damaging global settings. Keep in mind that the modified htaccess file will only affect the directory it is in, and makes damage control much simpler.
Create an .htaccess file in the root directory and append. The difference between htaccess and httpd.conf is how the file is accessed. The htaccess route is better for controlling specific directories or fixes to the Apache setup. The downside is that since the htaccess file is called for each page load, it can add extra load to the server.
To get a better understanding of how .htaccess files affects server directories, take a look at the excerpt from the Apache documentation:
The configuration directives found in a .htaccess file are applied to the directory in which the .htaccess file is found, and to all subdirectories thereof. However, it is important to also remember that there may have been .htaccess files in directories higher up. Directives are applied in the order that they are found.
According to the official Apache documentation, best practice dictates that the majority of directives should be made through the configuration files, and not htaccess wherever possible. The definitions found in httpd.conf are loaded when the server starts instead of on individual access, which may offer a slight difference in resources required. Keep in mind that any edits made to the .conf file will not take affect until the server has been restarted.
The process for editing your server’s httpd.conf file will vary based on hosting provider. This blog is hosted on a dedicated virtual server on MediaTemple, where the recommendation is to edit vhost.conf file in place of the main httpd.conf file. Refer to your hosting provider’s support documents for more. If you are unable to edit this configuration directly, the htaccess file may be the next best alternative.
Use in Moderation
You now have the tools and directions needed to create new file extensions on an Apache server. I strongly recommend against making changes for the hell of it. While this may be a nice trick, the average website probably doesn’t need a creative mix of new filenames (e.g. Ars Technica and .ars files) to suddenly show up. Arbitrarily applying this to your site would cause a considerable amount of confusion among search engines, users, and server configuration. Conventions exist for a reason.
Have you seen custom file extensions used effectively? Are you a server genius with some considerations to add to the discussion? Please share in the comments below.
Interested in finding out more about what Apache and htaccess modifications can do for your site? I’ve included a few links below to continue the research.