smarty output processor, mod_rewrite, and TikiWiki

UPDATED 11/18/04: Modified code to handle action= and other querystrings. This fixes a problem with the Lock and Unlock page links not working

TikiWiki is very nice. I used it to set up the Orlando Wiki and was very happy with everything you could do with it until Dan
looked a the wiki and said something to the effect of “good, now let's
rewrite those URLs to get good search engine rankings”. After a heated
IM debate on whether it mattered to Google if links to the wiki pages
were querystrings (e.g. or if they were SEF (search engine friendly) URLs (e.g.
I was swayed by Dan's persuasive argument (which boiled down to: you
are wrong and I know it) and decided to change the URLs to be SEF.

figured we could use mod_rewrite in a .htaccess file to rewrite the
incoming requests to something that TikiWiki can understand. This
turned out to be easy when I came across a post describing how to do this.
So, now we have the incoming links being rewritten, but what about the
links the TikiWiki spits out? Since it uses Smarty, we can hook into
the page processor and rewrite the output before it is sent to the
browser. Someone had already described how to do this, so I had a place to start.

you have to write a function that the Smarty output
filter can use. I used the code in the article above as a base. To
do what I wanted was much simpler–at least it was after battling to
get the correct regexp. Here's the code I used as the output filter:

function smarty_outputfilter_urlt($string) {
     $result = preg_replace('/<a(.*?)href="\"tiki\-index\.php\?page="(.*?)\"(.*?)"">/',
     // If the page was passed in an action, the link that was
     // 'tiki-index.php?page=foo&action=lock' will now look like 'foo&action=lock'
     // The first & needs to be converted to a ?
     $result = preg_replace('/(href=\"[a-zA-Z0-9]+?)(&amp;|&)/', '$1?', $result);
     return $result;

I saved that as outputfilter.urlt.php and uploaded it to the
lib/smarty/plugins directory. The next step was to add the following
line at the end of the code in setup_smarty.php in the root of the site:


This filter rewrites the URLs to be more SEF. Check out the home page of the Orlando wiki for an example.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s