Using Apache substitute with magento

  • Sumo

So I had a very interesting problem today. I needed to add a JavaScript file to all the sites hosted on my development server right before the head tag close.

Why? Because I wanted to get some RUM statistics without having to go into each site and change the header file.

Enter the Apache Substitute module

Apache substitute module is just the tool that I needed to do such a task. Think of it like a server implementation of “PHP’s preg_replace”

To enable it in Debian/Ubuntu:

a2enmod substitute

Now let’s do something with it:

vim /etc/apache2/mods-enabled/substitute.conf

Let’s say that we want to change the “


” to be “

<script type='text/javascript'>var i = time();</script></head>

Then adding this to our file will do that trick:

<Location />
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute "s|</head>|<script type='text/javascript'>var i =;</script></head>|ni"

Now this alone is not very useful:

So let’s add something else:

<Location />
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute "s|</head>|<script type='text/javascript'>var i =;</script></head>|ni"
    Substitute "s|</body>|<script type='text/javascript'>var p = - i; alert (p)</script></head>|ni"

Still not very useful but now you have a quick grasp what you can do with the Apache substitute module. Now you can tell how long

Now how is this useful for Magento? One simple use is to tag pages with something like this:

<Location ~ "^/(?!(admin|index.php/admin|js/tiny_mce))">
    AddOutputFilterByType SUBSTITUTE text/html
    Substitute "s|</body>|<div style='display:none;'>some content</div></body>|i"

Reload apache:

service apache2 reload

And watch the fruits of your labor!!

Ok, I still don’t see how useful this is? Well I didn’t want to install New Relic or Pingdom to measure the time to load for any given page in the frontend so this really came in handy.

PS: If you read RUM and thought of the beverage then you are my best friend!

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Author: Luis Tineo

Husband, Father, performance improvement junkie, biker and video gamer, Linux user and in my day job I'm a Software Engineer at BuyerQuest.

Share This Post On