Prepend and Append Files with .htaccess

By  on  

One of the lessor known and used capabilities of .htaccess files is the ability to prepend and append includes to every page request.  Doing so avoids needing to code <?php require('footer.php'); ?> in every template file you wat to use them in.  Here's the .htaccess code:

# Prepend the file
php_value auto_prepend_file "/dir/path/utilities.php"

# Append file to bottom of page
php_value auto_append_file "/dir/path/templates/footer.php"

Now don't mistake this post as me telling you to use this strategy;  using this functionality creates a layer of indirection that could confuse a team of developers if they don't all have a grasp of where automatically included files are coming from.  Just wanted to let you know this was possible!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Chris Coyier&#8217;s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    CSS :target

    One interesting CSS pseudo selector is :target.  The target pseudo selector provides styling capabilities for an element whose ID matches the window location's hash.  Let's have a quick look at how the CSS target pseudo selector works! The HTML Assume there are any number of HTML elements with...

  • By
    Chris Coyier: Some Amazing Work on CodePen III

    I'm back! David asked me to rope up some of my favorite stuff on CodePen again, which I both love doing, and wince at the thought of having to pick so few favorites. I like a ton of stuff on...

Discussion

  1. Markus

    this strategy is often used to hook a profiler into an existing app, which works great in DEV environments
    see e.g. http://xhprof.io/

  2. Barry Wood

    I can see it now, the /html directory full of code snippets, and an incredibly convoluted htaccess file that conditionally assembles the various pages for a site. :)

    • Quick! Turn it into a framework!

  3. I’ve used to use this technique, but dropped it for a while now.
    It appears very useful at first but can be dangerous actually…
    One of its weakness is the impossibility to “un” prepend or “un” append according to directory or filematch directives.

  4. Matthew

    How can i use the .httaccess pre-append a .php file? i have tried using—– php_value auto_prepend_file “inc.php”

    Am getting this error: Fatal error: Unknown: Failed opening required ‘inc.php” (include_path=’.;D:\xampp\php\PEAR’) in Unknown on line 0

  5. Abhijeet

    @Matthew

    have you tried using full path like /var/www/site/dir/file.php?

  6. .htaccess – is a single file for all project. And I think it’s a bad practice to prepend files, because they will prepend on all pages.

  7. I find it hard to believe @ Pluto htaccess allows if statements.

    php_value auto_prepend_file "fullpath/headers.php"
    php_value auto_append_file "fullpath/index.php"
    
  8. Thanks Chris, this would be a great way to append a “copyright link” that can’t easily be found.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!