Tag Archives: mod_substitute


As of 2.2.8, there’s a new module that comes standard with Apache HTTPd called mod_substitute. As soon as I upgraded to 2.2.8, I found an immediate use for mod_substitute.

mod_substitute lets you apply a regular expression to change data as it gets sent out to the client. Another module, mod_line_edit, has been around for a while and also does this. This is the first time it’s been part of the standard Apache distro.

We’re using TinyMCE to edit content on our website. Turns out that Internet Explorer doesn’t honor align=”left” and align=”right” on images, so we had to stick in style=”float: left” and style=”float: right” instead. But getting TinyMCE to do this was difficult since I don’t speak JavaScript fluently enough to make it do that. We created a class “float-left” and another “float-right” for this.

So, using mod_substitute, I did:

<Location />
AddOutputFilterByType SUBSTITUTE text/html
Substitute “s!(<img)(class=”float-(left|right)”)?([^>]*?)align=”(left|right)”([^>]*?)(class=”float-(right|left)”)?([^>]*>)!$1$4align=”$5″ class=”float-$5″$6$9!i”

Yes, that’s more complicated than it should be, because once you start editing and re-editing, the class=”float-*” ends up in the edited HTML, and we have to take it back off again. Sheesh.

But, that’s not all. When we load the content back into the editor, we have to take all that stuff off:

<LocationMatch (/admin/pages.php|/press/edit)>
AddOutputFilterByType SUBSTITUTE text/html
Substitute “s!<(img.*?)class=”float-(left|right)”(.*?)>!<$1$3>!i”

Cool, hmm?