// you’re reading...

PHP

Getting rid of /forums/ and /topic/ from bbPress permalinks – Updated Plugin

I was doing some work with bbPress recently and I was looking for a way to take the standard bbPress urls and turn them into something a little more hierarchical and semantic. Currently, they look like:

http://domain.com/forums/forum/name-of-forum/

http://domain.com/forums/topic/name-of-topic/

etc..

Not exactly ideal. It would be better if they looked more like:

http://domain.com/forums/name-of-forum/name-of-topic

So I found this post on the bbPress forums, which led me to this packaged plugin by Ashsh Mohta. However, a few things were busted – new posts, and also paging on forums and topic names that end with “forum/” or “topic/”

With a few modifications, I finally got it working, and here are the steps that I took. Note: This requires a bit of bbPress core hacking, but it’s literally only switching the order of two lines in the code.

  1. Enable name based permalinks in /bb-admin/options-permalinks.php
  2. Download the plugin from this page, put it in your my-plugins folder, and enable it
  3. Change the .htaccess file at the root of your bbPress install to the htaccess.dist file packaged with the plugin.
  4. Open up bb-post.php at the root of your bbPress install and simply switch lines number 46 and 48

I’m sure this isn’t perfect, and I’d love any and all feedback. It’s been working like a charm for me for a little while now, and hopefully it will help you! I’d love to find both a way to install this and have the plugin automatically backup and rewrite the .htaccess file, and of course a way to do it without editing the core code.

Plugin: Clean Up bbPress Permalinks

Discussion

11 comments for “Getting rid of /forums/ and /topic/ from bbPress permalinks – Updated Plugin”

  1. Number four in your instructions is missing text. It trails off after ‘and simply switch the…..’ ;)

    Kind of an important omission!

    Without first looking into the plugin, my suggestion for having it work without modifying core code is to make it core code. Submit it to trac as an enhancement request.

    Posted by mwaterous | September 19, 2009, 11:18 pm
  2. Hi, I’ve just followed your instructions and was also able to remove “forum” and “topic” from the urls.

    But something is wrong because the pages cannot be found.

    Probably I made something wrong with point four were you say that the lines 46 and 48 have to be switched. What do I actually have do do there?

    Is it this code?
    $link = get_post_link($post_id);

    $topic = get_topic( $topic_id, false );

    Posted by Alex | November 14, 2009, 1:27 pm
    • Hey Alex,

      Thanks for commenting. You are correct – those are the two lines and they should be switched and they should be something like


      $topic = get_topic( $topic_id, false );
      $link = get_post_link($post_id);

      Posted by mark | November 14, 2009, 4:16 pm
  3. I found a solution for the admin-backend link!

    Just ad the following code

    RewriteRule ^bb-admin/$ – [L]

    …before this code

    RewriteRule ^([^.]+)/([^.]+)/page/([0-9]+)/?$ topic.php?id=$2&page=$3 [L,QSA]

    ….and it works!

    Posted by Alex | January 24, 2010, 8:48 pm
  4. Hi! After 3 hours of brute-force testing, I found a working configuration:

    `
    # BEGIN bbPress

    Options -MultiViews

    RewriteEngine On
    RewriteBase /reactable-forum/

    #handle admin stuff
    RewriteRule ^bb-admin/.*$ – [L,QSA]

    # handle the rest
    RewriteRule ^page/([0-9]+)/?$ ?page=$1 [L,QSA]

    RewriteRule ^profile/([^/]+)/page/([0-9]+)/?$ profile.php?id=$1&page=$2 [L,QSA]
    RewriteRule ^profile/([^/]+)/([^/]+)/?$ profile.php?id=$1&tab=$2 [L,QSA]
    RewriteRule ^profile/([^/]+)/([^/]+)/page/([0-9]+)/?$ profile.php?id=$1&tab=$2&page=$3 [L,QSA]
    RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA]
    RewriteRule ^profile/?$ profile.php [L,QSA] # added

    RewriteRule ^rss/?$ rss.php [L,QSA]
    RewriteRule ^rss/topics/?$ rss.php?topics=1 [L,QSA]
    RewriteRule ^rss/forum/([^/]+)/?$ rss.php?forum=$1 [L,QSA]
    RewriteRule ^rss/forum/([^/]+)/topics/?$ rss.php?forum=$1&topics=1 [L,QSA]
    RewriteRule ^rss/topic/([^/]+)/?$ rss.php?topic=$1 [L,QSA]
    RewriteRule ^rss/tags/([^/]+)/?$ rss.php?tag=$1 [L,QSA]
    RewriteRule ^rss/tags/([^/]+)/topics/?$ rss.php?tag=$1&topics=1 [L,QSA]
    RewriteRule ^rss/profile/([^/]+)/?$ rss.php?profile=$1 [L,QSA]
    RewriteRule ^rss/view/([^/]+)/?$ rss.php?view=$1 [L,QSA]

    RewriteRule ^tags/([^/]+)/page/([0-9]+)/?$ tags.php?tag=$1&page=$2 [L,QSA]
    RewriteRule ^tags/([^/]+)/?$ tags.php?tag=$1 [L,QSA]
    RewriteRule ^tags/?$ tags.php [L,QSA]

    RewriteRule ^view/([^/]+)/page/([0-9]+)/?$ view.php?view=$1&page=$2 [L,QSA]
    RewriteRule ^view/([^/]+)/?$ view.php?view=$1 [L,QSA]

    # handle forums
    RewriteRule ^([^/]+)/page/([0-9]+)/?$ forum.php?id=$1&page=$2 [L,QSA]
    RewriteRule ^([^/]+)/$ forum.php?id=$1 [L,QSA] # no ‘?’ before the closing ‘$’ this time: very important!

    # handle topics
    RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ topic.php?id=$2&page=$3 [L,QSA]
    RewriteRule ^([^/]+)/([^/]+)/$ topic.php?id=$2 [L,QSA] # no ‘?’ before the closing ‘$’ this time: very important!

    # handle non-existent pages
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ index.php [L]

    # END bbPress

    Posted by Mr Pelle | April 8, 2010, 6:16 pm
  5. Why can’t I edit my own comments? I have to change `RewriteBase` to `/forum/`! =P

    Posted by Mr Pelle | April 8, 2010, 6:33 pm
  6. Hi!
    I’ve implemented 2 functions that grab both “plugin activated” and “plugin deactivated” events in order to swap .htaccess and the new file. Unfortunately you still have to change .htaccess permissions if you want the plugin to update it, but the only other way is manual edit…

    I’ve embedded them in the plugin: what do you think would be the better way to publish the updated plugin?

    Posted by Mr Pelle | April 9, 2010, 5:21 pm
  7. I don’t know how to contact you, but I sent an email to Ashish Mohta asking for his thoughts about the updated plugin.

    Posted by Mr Pelle | April 12, 2010, 1:33 pm
  8. No answer, so I made a plugin upload request today: “Nicer Permalinks” will be online soon! ^_^

    Posted by Mr Pelle | April 15, 2010, 5:53 pm

Post a comment