File Extension Rewrite for Apache

If you aren’t using a CMS/Blogging System (i.e. Drupal, WordPress etc.) then why not create a custom rewrite rule to strip your directory files of their extensions for your HTML5 Boilerplate .htaccess file. This customization allows your site to direct users by the plain old file name rather then having to add the file extension at the end. The result will give us www.mysite.com/thefile as opposed to www.mysite.com/thefile.html. Both ways can still be used, but the new default will now be the former rather than the latter. Since this customization is a personal preference to each project need, the HTML5 Boilerplate does not provide this rule by default in their .htaccessfile.

Regular Expressions ahead. Enter at your own risk!

To implement this server–side magic you will need the ability to edit your .htaccess file and insert the following code given below based on your desired configuration. If you are a Mac user then you are probably wondering why you can’t see the .htaccessfile.

TinkerTool for Mac

The reason is by default the Mac OS hides files such as .DS_Store or in our case .htaccess. In order for us to see and edit this .htaccess file we must enable hidden files by use of a great application I use called TinkerTool. TinkerTool allows us to activate hidden features in the Operating System and in some of the applications delivered with the system as well. To activate this feature from within TinkerTool simply check the box found next to “Finder Options” with the label “show hidden and system files.”

For further reading about URL Rewrites visit the Apache Project online documentation http://httpd.apache.org/docs/2.2/rewrite.

This post will be updated frequently as new techniques are implemented or changed as we evolve with the technology. If you have a great custom rewrite rule then please share it with the world by leaving your comments or suggestions.

SymbolDefinitionExample

Character Meanings
regular expression character meanings based on apache server project documentation. http://httpd.apache.org/docs/2.2/rewrite/intro.html
$ The other anchor, this matches the end of the string. a$ matches a string that ends with a.
. Matches any single character c.t will match cat, cot, cut, etc.
* Repeats the previous match zero or more times. a* matches all the same things a+ matches, but will also match an empty string.
^ Called an anchor, matches the beginning of the string ^a matches a string that begins with a
# ------------------------------------------------------
# strip .php files of file extension 
# ------------------------------------------------------ 
# rewrite strictly for files ending in .php 
# Instead of your sites files as this => domain-name.com/file.php 
# Rewrite so users can do this instead => domain-name.com/file 

<IfModule mod_rewrite.c> 
RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [L] 
</IfModule>

Dennis Gaebel

Design Technologist passionate for Open Source, SVG, Typography, Web Animation, Interaction Development & Pattern Based Design. http://droidpinkman.io.

Leave a Reply

Your email address will not be published. Required fields are marked *

show formatting examples
<pre class="language-[markup | sass | css | php | javascript | ruby | clike | bash]"><code>
…code example goes here…
</code></pre>

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview

  1. John Doe shouted this comment preview:
    2012/01/01