Spiga

[php class review] Relink – Rewrite URLs based on mod_rewrite configuration

November 02, 08 by Gabi Solomon

view this article in romanian

I have an email subscription to phpclasses email newsletter, and read it to see what new classes have been added to the site. And once in a while i will find interesting and innovative classes. Like today when between the classes that won the Innovation award was one that caught my attention.

The class developed by Benjamin Falk that he called Relink is a very interesting and useful tool in my opinion.

Lets say that you have a website and after you build it you want to make some SEO for it. That usually involves modifying all the links in the website. And that is quite a task. But what if you are like me and know a few things about SEO but are no expert, and somewhere down the line you either hire a SEO expert or receive an expert advice and want to change all those links ? Another time spent on tedious task of find and replace. And if we are talking about a larger project than that could be quite a big time.

Well what this class enables you to do is to make your links dynamically generated based on your .htaccess configuration. Let be more specific, first you must initialize the class

PHP:
  1. require_once 'class.relink.php';
  2.  
  3. $htaccessFile    = './htaccess-example';
  4. $c_relink        = new RELINK($htaccessFile);
  5.  
  6. // If you leave the $htaccessFile empty it will automatically take the .htaccess file from the current directory.

Then everywhere in the script where you have a link you will put something like

PHP:
  1. echo '<a href="'.$c_relink-&gt;replaceLink('?page=blog').'">View blog</a> | ';
  2. echo '<a href="'.$c_relink-&gt;replaceLink('?page=blog&amp;mode=edit').'">Edit Blog</a> | ';
  3. echo '<a href="'.$c_relink-&gt;replaceLink('?info&amp;value=all').'">Show all information</a>';

I didn't use it in any projects so far but I think i will do it on my next project.
Hope this new class is as interesting to you as it was to me.

Cheers