B2Evolution: Remove dates from permalinks hack

Here is a little hack to remove the dates from the permalinks, so that they will appear as http://www.example.com/index.php/posttitle instead of http://www.example.com/index.php/2005/12/12/posttitle.

These instructions are for B2Evolution 1.6, since there is an existing hack for older versions.

First edit the file evocore/_blog_main.inc.php
At around line 241 you will find this code:

                                { // We consider this a week number
                                        $w = substr( $path_elements[$i], 1, 2 );
                                }
                        }
                        else
                        {       // We did not get a number/year...
                                $path_error = 404;
                        }

Change it to:

                                { // We consider this a week number
                                        $w = substr( $path_elements[$i], 1, 2 );
                                }
                        }
                        else if( isset( $path_elements[$i] ) && (!empty( $path_elements[$i] )) )
                        { // We'll consider this to be a ref to a post
                                // We are accessing a post by permalink
                                // Set a lot of defaults as if we had received a complex URL:
                                $m = '';
                                $more = 1; // Display the extended entries' text
                                $c = 1;    // Display comments
                                $tb = 1;   // Display trackbacks
                                $pb = 1;   // Display pingbacks

                                if( preg_match( "#^p([0-9]+)$#", $path_elements[$i], $req_post ) )
                                { // The last param is of the form p000
                                        // echo 'post number';
                                        $p = $req_post[1];              // Post to display
                                }
                                else
                                { // Last param is a string, we'll consider this to be a post urltitle
                                        $title = $path_elements[$i];
                                        // echo 'post title : ', $title;
                                }
                        }
                        else
                        {       // We did not get a number/year...
                                $path_error = 404;
                        }

If that code looks familar, it is because it was copied from the code at line 217 in that same file. That is code that is normally used after tearing off the Y/M/D.
That takes care of parsing the urls to get to the right post. Now we need to change the permalink generation. Edit the file evocore/_item.class.php in the function gen_permalink, near line 2427.

Find this code:

                                        // This is THE CLEANEST available: RECOMMENDED!
                                        $permalink = url_add_tail( $blogurl, mysql2date("/Y/m/d/", $post_date).$urltail );

Change it to:

                                        // This is THE CLEANEST available: RECOMMENDED!
                                        $permalink = url_add_tail( $blogurl, '/'.$urltail );

9 thoughts on “B2Evolution: Remove dates from permalinks hack”

  1. Not bad. It’s quick and not too difficult to impliment after a new version release. Have is been tested on a b2evo install that hosts multiple blogs across multiple domains?

  2. The old date-style permalinks will still work just fine. I suppose you could go in and put in redirects, but I didn’t bother, since I implemented this change before any of my permalinks were released into the wild.

  3. is it somehow possibe to add a 503 (site moved) so already existing permalinks in the “old” date-style?

  4. Do you use this in 1.8.2 ?
    Because for the naked eye, it does what it is suppose to do, but there are a few downsides.
    Very odd, but registering new users is down…
    Is this only my installations (5) or do you have that problem to ?

  5. This still works as of version 1.9.2, with a couple of minor differences:

    The first file to be changed is now:
    /inc/_blog_main.inc.php
    This should be altered as described in the main post.

    The second is file is now:
    /inc/MODEL/items/_item.functs.php
    The function that needs editing is get_permalink (for some reason the archives plugin currently uses the deprecated get_permalink rather than its replaced version get_permanent_url).

    I have never done any php before so I reckon this hack is probably a real mess, but it works :)

    Find the bit of code that the main post tells you to change, and alter it to:
    //This is THE CLEANEST available: RECOMMENDED!
    $permalink = $file.mysql2date("", $postdata['Date']).'/'.strtolower($title);

  6. I’m using 1.9.3 and had to make two modifications:

    The main post modification for /inc/_blog_main.inc.php is still correct.

    I changed /inc/MODEL/items/_item.funcs.php to:


    //This is THE CLEANEST available: RECOMMENDED!
    $permalink = $file.mysql2date("", $postdata['Date']).'/'.$anchor;

    I also wound up changing the archives plugin file /plugins/_archives.plugin.php (around line 243).

    Here’s the original code:


    { // We want to link to the absolute canonical URL for this archive:
    echo get_permalink( $Blog->get('url'), $post_ID, 'id' );
    }

    I changed the ‘id’ parameter to ‘title’.


    { // We want to link to the absolute canonical URL for this archive:
    echo get_permalink( $Blog->get('url'), $post_ID, 'title' );
    }

    I don’t have my site converted to b2e yet. Just learning on a local install first.

Comments are closed.