Sunday, March 17, 2013

Gotcha of the Day: Handling requests to a long gone wiki

My friend George and I had a shared client with an interesting Search Engine Marketing opportunity. Years ago the site had hosted a wiki, but now those links were being sent to 404 pages. Something like:

George and I talked it through. What would be the best user experience for folks who followed those old links? Surely we could do better than a 404 page. Because the topic of the website hadn't changed over all these years, we decided that a clever approach would be to turn those Wiki requests into site searches. Users wouldn't see the old Wiki, but they'd be taken to relevant content on the site.

Implementing that solution from a technical perspective wasn't terrifically hard. The first step was to make a small script I called wiki_resolver.php. Here's the code:

 * A PHP file for handling old requests to the Wiki.

$query = str_replace('_', ' ',                      // [A]
                     substr($_SERVER['REQUEST_URI'], 6));
$qs    = http_build_query(array('q' => $query), 
                          false, '&');

header("Location: /search?$qs", true, 301);         // [B]

[A] turns the Wiki URL parameter into a search engine friendly string and [B] sends the user to the search page. Notice the 301 redirect, the preferred redirect for this scenario; that's thanks to George's attention to detail.

The second and final step was to redirect /wiki/ request to this script. I did this via my beloved Apache Rewrite Rules. The one line of code we needed to add to the .htaccess file was:

  RewriteRule ^wiki/(.*)        /tools/wiki_resolver.php    [L]

And that was it; requests to /wiki are now gracefully handled.

I love SEO solutions like this one that put human visitors first, and robot visitors second. With all the attention paid to pleasing Google and related services, it's often the actual visitors who are left out of the equation. Not this time.

No comments:

Post a Comment