FlexURL for WordPress 1.2

FlexURL is a plugin for WordPress users to allow broken long URLs to work in spite of their broken-ness. But it only works on WordPress 1.5.x.

For Wordpress 1.2 users, the fix is not as simple as a plugin, but it is only a one-line code change. Here’s how to implement it on your own blog:

  1. Go to your blog directory and make a backup copy of wp-blog-header.php, like so:
    cp wp-blog-header.php wp-blog-header.php.bak

  2. Edit the wp-blog-header.php file in your favorite text editor:
    vi wp-blog-header.php

  3. Find this section of code (search for “AND post_name”). I found it on line 191:
    $where .= " AND post_name = '$name'";

  4. Change this line by replacing “=” with “LIKE” and adding a percent sign after $name, like so:
    $where .= " AND post_name LIKE ‘$name%‘”;

  5. Save it and get out.

Make sure you don’t accidentally add a blank line to the end of this file. Some (pico?) editors will do this automatically. Avoid them. If a blank line gets added, it may causes PHP errors saying something like “Unable to alter headers after output on line …” If this happens, fix the file by deleting the blank line or blank spaces at the end of the file. Or go to your backup file from step 1:
cp wp-blog-header.php.bak wp-blog-header.php

[Edit: 06/08/2005]
To make the browser show the corrected URL in the address bar instead of the broken one, do this:

1. Find this section at the very end of wp-blog-header.php:

if (1 == count($posts)) {
    if ($p || $name) {
        $more = 1;
        $single = 1;
    }
    if ($s && empty($paged)) { ...

2. Insert the two lines below (in bold) so it looks like this:

if (1 == count($posts)) {
    if ($p || $name) {
        $more = 1;
        $single = 1;
    }

    if (stristr(get_permalink($posts[0]->ID), $name.'/') === FALSE)
    HEADER ( 'Location: ' . get_permalink($posts[0]->ID) );

    if ($s && empty($paged)) { ...

5 Responses to “FlexURL for WordPress 1.2”

  1. phord Says:

    Update: Someone told me that on WP 1.2.1 this change makes the *newest* matching post come up, and not the short versions of each post. That’s fine for her, but it’s different than FlexURL.

    Let me know what happens on your 1.2.1 site. And if you know how to “fix” it to show a list, please feel free to contribute here.

  2. jez Says:

    As for me it doesn´t work on wordpress 1.5 and higher.
    They have probably changed the content of wp-blog-header.php, because I wasn’t able to find the parts you mentioned.
    I love the idea of flexURL-
    Do you plan to publish a new version that works with wp 1.5 or even 2.0 (whenever that will be released) soon or later?

  3. phord Says:

    FlexURL works on WP 1.5. It’s not here… it’s here:
    http://www.philhord.com/phord/wordpress-versus-the-long

  4. wizzart Says:

    Can this work on non wordpress sites?

  5. phord Says:

    The principle works on non-WP sites, but this plugin is for WordPress only.

    Ask-Leo does something similar for MoveableType, for example. I don’t know how he does it on his side. Maybe you can go Ask-Leo. :-)
    http://ask-leo.com/watch_the_cock

    It’s possible to do it all in .htaccess, but this plugin makes the process automatic for WP.

Leave a Reply