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.

http://buildinternet.com/live/custom-files/index.omr

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.

.htaccess

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.

httpd.conf

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.

Further Reading

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.

  1. Internet Media Types on Wikipedia
  2. Tutorial on editing httpd.conf for MediaTemple
  3. The htaccess Cheat Sheet
  • Stumble It!
  • Bookmark It!
  • Tweet it!

About Zach Dunn

Zach is a partner and interface designer at One Mighty Roar from Massachusetts, USA. Follow him on Twitter @zachdunn.

 

Discussion

  1. Hitesh Chavda

    March 11th, 2010 at 11:36 PM

    of course,

    OMR ( Optical Mark Reader)

  2. Max

    March 12th, 2010 at 4:32 AM

    Of courde, OMR = Oh My Russian… is so horrible ))

  3. Arvind

    March 12th, 2010 at 6:38 AM

    It is indeed a good technique. The reasons – It is gives a higher points in security (very little actually as the attacker wont know the script language used in once glance). Second While declaring html as php and using a good cache header you’ll increase performance

  4. Jonathan

    March 12th, 2010 at 8:07 AM

    Very nice.

    However, security through obscurity is not security at all.

  5. Josh

    March 12th, 2010 at 10:20 AM

    Neat trick! I might use that someday. :)

  6. Amberly | Web Designer

    March 12th, 2010 at 11:15 AM

    Interesting read.

  7. Alex

    March 12th, 2010 at 3:16 PM

    I’ve seen this used on a few corporate sites my agency has worked on for language changes. We’ll often have an index.en and an index.fr for english and french.

  8. Ryan Hamilton

    March 13th, 2010 at 3:01 AM

    Gonna take a shot in the dark and say that .omr is referring to One Mighty Roar. Great read.

  9. Michele

    March 13th, 2010 at 10:13 AM

    One of the largest property sites in Ireland uses custom extensions like this and has done for years:
    http://www.daft.ie/

    I’m not sure what benefit, if any, it brings, but it can have its uses if you need to use .html files but want them to be able to parse php, for example

  10. Saddam Azad

    March 13th, 2010 at 3:04 PM

    I fail to see the point of custom file types used for websites. The .ars extension on Arstechnica might as well have been .html (not that the readers would notice or care)

    It might a good idea to use .html for SEO purposes but inventing obscure extensions is rather silly, me thinks.

  11. Vamsi

    March 13th, 2010 at 9:43 PM

    Hi,
    has this got any adverse effect on SEO ?

  12. Zach Dunn

    March 14th, 2010 at 9:52 PM

    @Vamsi

    That’s a good question, and truthfully I don’t know. I’d imagine that part of the search engine algorithm evaluates the file type for each page. If it’s a custom one, the crawler may eventually figure it out, but it probably doesn’t make it easy.

  13. Atul Kash

    March 16th, 2010 at 6:01 PM

    I guess this is strictly for new projects. I’ll have to modify all my links if I were to implement this.

  14. Michal

    March 18th, 2010 at 6:17 AM

    Hi, I found many informations about file extensions at file-extensions.org.

  15. Brian

    March 23rd, 2010 at 9:34 AM

    @Vamsi
    You can actually use these tricks to preserve SEO, in conjunction with mod_rewrite.

    Say you’re given a website of highly ranked static HTML files and wanted to use PHP instead. Without having to change file extensions or file names, you could change PHP as the handler of HTML files. It’s all transparent to the end user and will not affect your rankings.

    Or you could rewrite certain URLs of HTML files to another name or file extension, if you used mod_rewrite.

  16. steve

    May 27th, 2010 at 3:01 PM

    very good for hacking sites, huh?
    you get access to a site, modify the .htaccess to make .jpg or .gif executable as php.

    leave behind a well placed jpg (with some appended php code)
    and voila!

    heck, you could even have the php code execute via a img tag

  17. John Media@dedicated server

    June 8th, 2010 at 9:26 AM

    Wow this is really informed me a lot and learned many things from this post and using this seems interesting, glad to share your techniques Thanks.

  18. Kent

    July 3rd, 2010 at 7:35 AM

    Thanks, very cool…

  19. Seon Poppcile | web designer

    July 27th, 2010 at 9:27 AM

    nice read! think i’ll bookmark this one for later use. Thanks!

Join the Conversation!

Remember: Life's not all doom and gloom, so please keep it constructive. If we've made an error or missed something big, please let us know! Learning is revisions, after all.

CommentLuv is Enabled

 

Sponsors

Advertise on Build Internet!