May 11, 2005

SSI - Regular Expressions

Apache

Since the advent of Apache 2.0.x you have been able to do regular expressions in apache ssi. Meaning less reliance for me at least on .htaccess files and mod_rewrite.

This has lead me to be able to quickly make template files that pass the query string in multiple parts to the page to dynamically change the content.

Hence we have the Benjamin Gallery and examples of the query string in use (bits echoed in red)-

http://www.cight.com/benjamin/gallery/august/ tmpl.shtml?src=big/big_img_2421.jpg; comm=Flashdance%20Benjamin

Using code as follows

<!--#if expr="$QUERY_STRING = /src=([^;]*)/" --><!--#set var="picture" value="$1" --><!--#endif -->

- find the src= bit find and capture what is after it, until you find a ; then set that as a variable called picture.

<!--#if expr="$picture = /([0-9][0-9][0-9][0-9])/" --><!--#set var="number" value="$1" --><!--#endif -->

- take the pciture variable now created, and look in it for 4 digits, save that as a variable called number.

<!--#if expr="$QUERY_STRING_UNESCAPED = /comm=([^;]*)/" --><!--#set var="comment" value="$1" --><!--#endif -->

- find the comm= bit find and capture what is after it, until you find a ; then set that as a variable called comment. Unfortunately if you use punctuation this ends up escaped if you then echo it, meaning ? becomes \?. And a weirdness in apache won't let you get rid of the \.

Then you can use the variables you have to put comments on the page, link to the larger version (like in the example) and back without chnaging template.

Also it makes the gallery page very easy to create, you just put in the urls of the images and wrap template code to each one (go view source)

Posted by nickh at May 11, 2005 04:44 PM

Email this entry



Comments