walls.corpus

By Nathan L. Walls

  • Sunset, Jan. 2, 2021/Williams Township
  • On Bougher Hill/Williams Township
  • Sunrise, Dec. 19, 2020/Williams Township
  • Sunset, Dec. 27, 2020

Apache Server Side Include directives for local web development

I’m developing some JavaScript to interact with a Rails service I have in support of CrazyLikeThat.com. I’m an infrequent jQuery developer, so I spend a lot of time in WebKit’s Web Inspector. I also use Google Analytics and Google AdSense. In the middle of looking at the Web Inspector console, I noticed a blizzard of warnings and errors from Google’s activity.

I remembered a few years back to some time at sacbee.com when one of the designers demonstrated Apache’s server side includes weren’t limited to including a page snippet or executing a CGI script, but included conditional logic. That’s precisely what I was after in this case.

I set an environment variable in my local Apache configuration then wrapped the conditional logic around my AdWords call and the Analytics JavaScript. In 10 minutes, I had a nice, quick solution to keeping the Web Inspector clean of anything other than my own errors.

So, what does this look like? My Apache directive is very simple. You’ll need both mod_include and mod_env enabled.

Then, on my pages, I do something like this:

When that NoAds environment variable is set, that file won’t be included. In production, it will be. My next approach on this will be to use different versions of jQuery based on environment. I can use a developer friendly version for my local development and use Google’s hosted jQuery in production. What’s really nice about this is with Apache 2.2’s chained handlers, I can use the same logic regardless of whatever’s building the page first, be it Rails, Template Toolkit or PHP.