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. http://orlando.citywikis.com/tiki-index.php?page=HomePage) or if they were SEF (search engine friendly) URLs (e.g. http://orlando.citywikis.com/HomePage)
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.

I
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.

Basically,
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="(.*?)\"(.*?)"">/',
        '<a$1href="$2"$3>',$string);
     // 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:

$smarty->load_filter('output','urlt');

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:

WordPress.com Logo

You are commenting using your WordPress.com 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