Skip to:
Content
Pages
Categories
Search
Top
Bottom

nicer slug url rewrite plugin (done!)

  • Hi all.

    (Sorry for my bad english. I tried to be verbose in order to avoid my bad english affect your understanding)

    I wrote a plug-in, in order to have the best rewrite rules (in my opinion :p ) in the world.

    this plugin achieves two things:

    One: I need to show, in the URL, the logical parent-to-child relation between forums and topics, so

    http://www.example.com/bbpress/topic/my-sweet-dog

    URL has to show the forum that contains the topic, and become

    http://www.example.com/bbpress/forum/pets-discussions/topic/my-sweet-dog

    when performing the rewrite, the rules totally ignore the forum part, so it’s only a visual thing.

    and, more difficult,

    Two: I need to shorten the URL and have fewer subdirectories, so

    http://www.example.com/bbpress/forum/pets-discussions/topic/my-sweet-dog

    has to lose the useless “/forum/” and the useless “/topic/” parts, and become

    http://www.example.com/bbpress/pets-discussions/my-sweet-dog

    ATTENTION: you cannot do the second thing without the first thing. you must be able, in rewrite rules, to discriminate between a topic and a forum. you then will have that forums have one string (the forum slugged name), while topics have two strings (the forum slugged name and the topic slugged name), so you can do that, and /pets-discussion/my-sweet-dog will belong to “My Sweet Dog” topic in “Pets Discussions” forum

    In order to do so, I need to

    – modify forum link creation

    – modify topic link creation

    – modify rewrite rules

    – avoid a forum to have a reserved name, as using bbpressfolder/forumname to reach a forum is risky! I have to prevent a forum from having a slugged name like “bb-admin”!!! here’s a list of reserved words:

    – all words with a dot. those are files, like style.css and so on. the slugged names never have a dot. I have only to avoid rewrite of dotted words.

    – all bbpress subfolders and special folders. these are all that start with

    bb-* , my-*

    – every other reserved word already used by rewrite engine:

    tag, profiles, view, rss

    All this is achieved adding a “r-” string in front of the slugged string. so a forum named “My Forum!” will have “r-my-forum” as slugged name. Unfortunately, this filter operates also for topic names and maybe other things. this mean that a topic named “rss” will have “r-rss” as slugged name. Not so bad, in facts.

    So these are the operations:

    – add a filter to get_forum_link (delete the “/forum” part)

    – add a filter to get_topic_link (add the “forum/slug-forum-name” in the link, and eliminate the “/forum” and “/topic” parts)

    – modify the rewrite rules (I used IIS isapirewrite, but apache is pretty the same)

    – add a filter to bb_slug_sanitize (avoid a forum to have a reserved word as slugged-name, like “rss”, “profiles”, “my-plugins”, etc

    And there is the code for these operations:

    function my_get_forum_link_filter( $link , $forum_id = 0 ) {
    //retrieve the forum object
    $forum = get_forum( get_forum_id( $forum_id ));

    //check for rewrite
    $rewrite = bb_get_option( 'mod_rewrite' );
    if ( $rewrite ) {
    //what kind of rewrite there is? slug use "forum_slug" column, else the column is "forum_id"
    $column = ($rewrite === 'slugs')?('forum_slug'):('forum_id');

    // change /forum/pets-discussions in /pets-discussions
    // this work only if the rewrite module is modded!
    // and this work only if the slugged name will NEVER
    // be a reserved word like "rss" or "bb-images"
    // and this is achieved by a filter on bb_slug_sanitize
    $link = str_replace('forum/' . $forum->$column , $forum->$column, $link);
    }
    return $link; // Very important line!
    }

    add_filter( 'get_forum_link', 'my_get_forum_link_filter' );

    function my_get_topic_link_filter( $link, $topic_id = 0) {
    //retrieve the topic object
    $topic = get_topic( get_topic_id( $topic_id ));

    //retrieve the forum object that is the topic container
    $forum = get_forum( get_forum_id( $topic->forum_id ));

    //check for rewrite
    $rewrite = bb_get_option( 'mod_rewrite' );
    if ( $rewrite ) {
    //what kind of rewrite there is? slug use "forum_slug" column, else the column is "forum_id"
    $column = ($rewrite === 'slugs')?('forum_slug'):('forum_id');

    //create the "forum/pets-discussions" chunk to show the hierarchical relation forum->topic
    $forum_nice_uri = "forum/" . $forum->$column . "/";

    //attach the hierarchical chunk to the link
    $link = str_replace(bb_get_option('uri'), bb_get_option('uri') . $forum_nice_uri, $link);

    // change /forum/pets-discussions/topic/my-sweet-dog in /pets-discussions/my-sweet-dog
    // this work only if the rewrite module is modded!
    // and this work only if the slugged name will NEVER
    // be a reserved word like "rss" or "bb-images"
    // and this is achieved by a filter on bb_slug_sanitize
    $link = str_replace('forum/' . $forum->$column , $forum->$column, $link);
    $link = str_replace('topic/' . $topic->$column , $topic->$column, $link);
    }

    return $link; // Very important line!
    }

    add_filter( 'get_topic_link', 'my_get_topic_link_filter' );

    function my_bb_slug_sanitize_filter( $text_slug, $text_original = '', $length = 0 ) {
    // add "r-" by regex when the string begins with "bb-" or "my-" or is a reserved word
    return preg_replace('/^(my-.*|bb-.*|rss|tags|view|profiles)$/', 'r-$1', $text_slug);
    }

    add_filter( 'bb_slug_sanitize', 'my_bb_slug_sanitize_filter' );

    And there’s the rewrite rules! ATTENTION!!!!! these rules are for IIS isapirewrite, so these are to be modded to work with apache! sorry but I don’t have any apache installation to play with. It’s a simple task, anyway. Every regex guru and regex accolite can do that. Even a regex wannabe can find out googling.

    #	first we rewrite the pages for normal slug-rewrite usage

    RewriteRule /bbpress/tags/([^/?]+)/page/([0-9]+)(?:?(.*))? /forum2/tags.php?tag=$1&page=$2?3&$3: [I,L]
    RewriteRule /bbpress/tags/([^/?]+)/?(?:?(.*))? /forum2/tags.php?tag=$1?2&$2: [I,L]
    RewriteRule /bbpress/tags/?(?:?(.*))? /forum2/tags.php(?1?$1:) [I,L]
    RewriteRule /bbpress/profile/([^/?]+)/page/([0-9]+)(?:?(.*))? /forum2/profile.php?id=$1&page=$2?3&$3: [I,L]
    RewriteRule /bbpress/profile/([^/?]+)/([a-z-]+)(?:?(.*))? /forum2/profile.php?id=$1&tab=$2?3&$3: [I,L]
    RewriteRule /bbpress/profile/([^/?]+)/([a-z-]+)/page/([0-9]+)(?:?(.*))? /forum2/profile.php?id=$1&tab=$2&page=$3?4&$4: [I,L]
    RewriteRule /bbpress/profile/([^/?]+)/?(?:?(.*))? /forum2/profile.php?id=$1?2&$2: [I,L]
    RewriteRule /bbpress/view/([a-z-]+)/page/([0-9]+)(?:?(.*))? /forum2/view.php?view=$1&page=$2?3&$3: [I,L]
    RewriteRule /bbpress/view/([a-z-]+)(?:?(.*))? /forum2/view.php?view=$1?2&$2: [I,L]
    RewriteRule /bbpress/rss/(?:?(.*))? /forum2/rss.php?1&$1: [I,L]
    RewriteRule /bbpress/rss/forum/([0-9]+)(?:?(.*))? /forum2/rss.php?forum=$1?2&$2: [I,L]
    RewriteRule /bbpress/rss/topic/([0-9]+)(?:?(.*))? /forum2/rss.php?topic=$1?2&$2: [I,L]
    RewriteRule /bbpress/rss/tags/([a-z-]+)(?:?(.*))? /forum2/rss.php?tag=$1?2&$2: [I,L]
    RewriteRule /bbpress/rss/profile/([0-9]+)(?:?(.*))? /forum2/rss.php?profile=$1?2&$2: [I,L]

    # then we have a rule for special words, so they are left as they are, and the isapi module does not proceed further

    RewriteRule /bbpress/(my-.*|bb-.*|rss|tags|view|profiles)(/.*)? /forum2/$1$2 [I,L]

    # then we have the forum and topic rules.
    # ATTENTION: we DO NOT rewrite a dottet word, because dotted words can be files like style.css and so on

    # before there are the topic rewrites, that are longer
    # ATTENTION: note that the forum name is totally skipped trough ?: notation. In facts, bbpress doesn't need the forum name when reading a topic
    RewriteRule /bbpress/(?:[^./?]+)/([^./?]+)/page/([0-9]+)(?:?(.*))? /forum2/topic.php?id=$1&page=$2?3&$3: [I,L]
    RewriteRule /bbpress/(?:[^./?]+)/([^./?]+)/?(?:?(.*))? /forum2/topic.php?id=$1?2&$2: [I,L]

    # and after there are the forum rewrites, that are shorter
    RewriteRule /bbpress/([^./?]+)/page/([0-9]+)(?:?(.*))? /forum2/forum.php?id=$1&page=$2?3&$3: [I,L]
    RewriteRule /bbpress/([^./?]+)/?(?:?(.*))? /forum2/forum.php?id=$1?2&$2: [I,L]

    And that’s all. I hope someone can use this plugin :)

    Gus

    email: shiawase at gmail dot com

Viewing 23 replies - 1 through 23 (of 23 total)
  • Has anyone tried this?

    haven’t, but it looks reasonable.

    it is currently active on tour2000.it/forum (it’s a BIG forum with multiple domains names (~20) and forum names).

    Help… I don’t understand anything :(.

    I read step by step your instruction and do all what do you wrote.

    But at the and I got: “The requested URL /<forum-home-dir>/<forum-slug> was not found on this server.”

    What I do wrong?

    Yesssss I did it!!!

    Instructions for Apache users:

    1. Append to <bbpress-root>/bb-includes/template-functions.php before “?>” code from first message in this topic.

    2. Create or edit .htaccess file in <bbpress-root> directory and put into then next code

    Options +FollowSymlinks

    RewriteEngine On

    RewriteBase /!!!!!! your bbpress root dir!!!!!!!/ #edit this!!!!

    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 ^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 ^view/([^/]+)/page/([0-9]+)/?$ view.php?view=$1&page=$2 [L,QSA]
    RewriteRule ^view/([^/]+)/?$ view.php?view=$1 [L,QSA]
    RewriteRule ^rss/?$ rss.php [L,QSA]
    RewriteRule ^rss/forum/([^/]+)/?$ rss.php?forum=$1 [L,QSA]
    RewriteRule ^rss/topic/([^/]+)/?$ rss.php?topic=$1 [L,QSA]
    RewriteRule ^rss/tags/([^/]+)/?$ rss.php?tag=$1 [L,QSA]
    RewriteRule ^rss/profile/([^/]+)/?$ rss.php?profile=$1 [L,QSA]

    RewriteRule ^bb-login.php$ index.php

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

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


    aiitee
    Member

    @motaboy21

    should i add the first code before the ?> right? because that didnt worked out for me… and the mod rewrite rules i added to the .htacess file, i have some coding there:

    Options +FollowSymLinks +ExecCGI

    <IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteBase /

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

    RewriteRule ^forum/([^/]+)/?$ /forum.php?id=$1 [L,QSA]

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

    RewriteRule ^topic/([^/]+)/?$ /topic.php?id=$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 ^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 ^view/([^/]+)/page/([0-9]+)/?$ /view.php?view=$1&page=$2 [L,QSA]

    RewriteRule ^view/([^/]+)/?$ /view.php?view=$1 [L,QSA]

    RewriteRule ^rss/?$ /rss.php [L,QSA]

    RewriteRule ^rss/forum/([^/]+)/?$ /rss.php?forum=$1 [L,QSA]

    RewriteRule ^rss/topic/([^/]+)/?$ /rss.php?topic=$1 [L,QSA]

    RewriteRule ^rss/tags/([^/]+)/?$ /rss.php?tag=$1 [L,QSA]

    RewriteRule ^rss/profile/([^/]+)/?$ /rss.php?profile=$1 [L,QSA]

    </IfModule>

    should i delete this and copy the one you gave me?


    _ck_
    Participant

    @_ck_

    If this is known working, it’s useful as a few people have requested the ability to remove /forum/ and /topic/

    The author should be encouraged to add this to the plugin browser:

    https://bbpress.org/plugins/requests/

    I just found a blog post about this, where the author has made the code into a plugin. It’s available for download here:

    How to remove forum and topic keyword from bbpress url

    The author lists two minor issues here, which hopefully someone here can suggest a fix for:

    • The Link to BBpress admin board link does not work from main site. It redirects to home page. You will need to manually type the link to go inside.

    • In case you type something which does not exists as http://sitename.com/<bbpress-directory>/sdaksda it returns http://sitename.com/<bbpress-directory&gt:// . An extra slash.

    I discovered another issue with this hack. If you write a new post in a new topic, after the submitting the form, you are redirected back to the forum’s front page, instead of the forum you were on. I spent some time digging around the get_post_link() related functions, and couldn’t figure out exactly what was going on. However, I was able to make this modification to the bb-post.php file to get it to redirect properly.

    Paste this after line 43, in bb-post.php of bbPress 0.9.0.2

    $link = str_replace('forums/', "forums/". $forum->$topic_slug, $link);

    Nevermind my last post. You can just swap the positions of line 43 and 45, in bb-post.php, and it works. Not sure why, but it does.

    the admin link can be brought to life by adding this:

    RewriteRule ^bb-admin/$ – [L]

    before this line:

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

    and to resurrect login, remove or comment out the line:

    RewriteRule ^bb-login.php$ index.php

    i don’t know why its there. perhaps its for something important that i don’t get.

    actually, the admin link only works if you login on the bbpress page.

    bummer

    I have it working ok with the plugin, but testing reveals it fails to work with multipage threads, eg:

    http://domain.com/forums/test/your-first-topic/page/2

    breaks and redirects to //

    I had a look at the rewrites and similar but it seems ok (though I am not an expert), so I am at a loss. Is anyone else having this problem? Any ideas on a fix?

    Has anyone got search results to work ok with this plugin?

    The post_link() on my search results are missing the base directory and forum slug (ie: they look like http://domain.tld//topic-slug#id)

    Also, the My Views plugins don’t seem to work properly, has anyone else noticed this?


    anandsrinivasan
    Member

    @anandsrinivasan

    Is there a ‘for dummies’ version of the same. I am programming-handicapped and can understand nothing of what is been discussed here, though I too want to achieve the same thing :O

    Thanks

    Hm, this doesn’t seem to work that well with bbPress 1.01 unfortunately. Posting new posts seem to go to hell and it just redirects one to FORUM-URL/

    Any updates?

    Forum:

    http://www.callofduty.se/forum/


    AphelionZ
    Participant

    @aphelionz

    I updated this plugin with some fixes – new posts, and paging. Also, I used a better way of removing the forum/ and topic/ from the urls that doesn’t involve str_replace.

    http://blog.markroberthenderson.com/getting-rid-of-forums-and-topic-from-bbpress-permalinks-updated-plugin/

    Point 4 in your linked page does not read in full, AphelionZ: it is cut…


    AphelionZ
    Participant

    @aphelionz

    Whoops! Sorry. Fixed.


    alekseo
    Member

    @alekseo

    Anyone got this working atm? And im also wondering if this is the way to remove the topic/ from individual posts and then insert the /forums/FORUMNAME/postname instead?

    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. This is my rewrite code (it is a bit different from your code because I am using the newest version of bbpress):

    # BEGIN bbPress

    Options -MultiViews

    <IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteBase /forum/

    Options +FollowSymlinks

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

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

    RewriteRule ^forum/([^/]+)/?$ /forum.php?id=$1 [L,QSA]

    RewriteRule ^forum/?$ [R=302,L,QSA]

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

    RewriteRule ^topic/([^/]+)/?$ /topic.php?id=$1 [L,QSA]

    RewriteRule ^topic/?$ /forum/ [R=302,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 ^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]

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

    RewriteRule ^view/([^/]+)/?$ view.php?view=$1 [L,QSA]

    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/?$ /forum/rss.php?tag=$1&topics=1 [L,QSA]

    RewriteRule ^rss/profile/([^/]+)/?$ rss.php?profile=$1 [L,QSA]

    RewriteRule ^rss/view/([^/]+)/?$ /forum/rss.php?view=$1 [L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^.*$ /forum/index.php [L]

    </IfModule>

    # END bbPress

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

    Quote: “4. Open up bb-post.php at the root of your bbPress install and simply switch lines number 46 and 48”

    Is it this code?

    $link = get_post_link($post_id);

    $topic = get_topic( $topic_id, false );


    chrishajer
    Participant

    @chrishajer

Viewing 23 replies - 1 through 23 (of 23 total)
  • You must be logged in to reply to this topic.