Search Engine Optimization (SEO) for phpBB URLs

I recently installed phpBB on HappyMudan.com, a new site my wife and I launched last week. I like phpBB, but it’s not the most search engine friendly bulletin board. I did a little Googling and found this site, which has a few optimization tips and which I highly recommend.

However, one thing it doesn’t include is tips on how to rewrite your urls into more meaningful names. As an example, it’s usually a good idea to have the title of a page in the name of the url because search engines give those keywords more weight. So, for example, we’d like the url http://www.happymudan.com/discuss/viewforum.php?f=4 (which looks pretty meaningless) to be something relevant to the keyword “immigration,” which is the topic for this forum. After a little hacking, I was able to come up with a url of the form http://www.happymudan.com/discuss/forum-Immigration-id_4.html. It’s not perfect, but it’s a lot more meaningful than before.

Fortunately, there are only a couple of places where these kinds of urls are really necessary – the listing of forums and the listing of topics.

As an example, if you have two forums – “derek’s news” and “funny jokes” – you want the urls to be something like:

/derek’s-news.html
/funny-jokes.html

Since phpBB processing all happens in the same directory, we have to add some hints to the urls to tell Apache which script to pass them to. So alter those urls to something like:

/forum-derek’s-news.html
/forum-funny-jokes.html

Further muddying the waters, phpBB allows two different forums to have the exact same name (ditto with topics). So we have to also put the unique forum ID into the url:

/forum-derek’s-news-id_93.html
/forum-funny-jokes-id_234.html

Lastly, forum and post names can have non-standard url characters in them (such as ‘ and &). So we have to urlencode them:

/forum-derek%27s-news-id_93.html
/forum-funny-jokes-id_234.html [no change on this one]

So, how do we do this? It’s actually pretty simple. There are two files to edit.

The first is index.php in the root of your installation. Search for:

$template->assign_block_vars(’catrow.forumrow’

Immediately above this code block, add the following:

//derek’s seo url hack
//use the forum name as part of the url
$forum_name=$forum_data[$j][’forum_name’];
$view_forum_url=trim($forum_name);
//prepend ‘forum’ so we know this url is for viewforum.php
$view_forum_url=’forum-’ . $view_forum_url;
//replace spaces with dashes
$view_forum_url=str_replace(’ ‘,’-‘,$view_forum_url);
//replace “/” with dashes – for some reason my (admittedly weak) Apache regex doesn’t like “/”
$view_forum_url=str_replace(’/’,’-‘,$view_forum_url);
//append -id_ as a handle for the regex to find the correct forum_id
$view_forum_url = (urlencode($$view_forum_url).”-id_$forum_id”;
//add .html to make it look like a static web page
$view_forum_url .=’.html’;
$view_forum_url=append_sid($view_forum_url);

A few lines below the $template->assign_block_vars section, you should see this:

‘U_VIEWFORUM’ => append_sid(”viewforum.$phpEx?” . POST_FORUM_URL . “=$forum_id”))

Comment this out and replace it with:

‘U_VIEWFORUM’ => $view_forum_url)

Save the file.

Next, open the file viewforum.php. Search for “$view_topic_url = ” without the outer quotes.

Comment out this line using //. Immediately above this code block, add the following lines:

//derek’s seo hack
//use the topic title as part of the url
$view_topic_url=trim($topic_title);
//prepend ‘topic’ so we know this url is for viewtopic.php
$view_topic_url=’topic-’ . $view_topic_url;
//replace spaces with dashes
$view_topic_url=str_replace(’ ‘,’-‘,$view_topic_url);
//replace “/” with dashes – for some reason my (admittedly weak) Apache regex doesn’t like “/”
$view_topic_url=str_replace(’/’,’-‘,$view_topic_url);
//append -id_ as a handle for the regex to find the correct forum_id
$view_topic_url = urlencode($view_topic_url).”-id_$topic_id”;
//add .html to make it look like a static web page
$view_topic_url .=’.html’;
$view_topic_url=append_sid($view_topic_url);

Save the file. We’re almost done.

Add the lines below to your .htaccess. NOTE: this code assumes your phpBB is in a subdirectory called phpBB. Change the “phpBB” to whatever directory you use, or remove it altogether if phpBB is in your document root:

RewriteEngine On
RewriteRule ^/phpBB/topic-(.*)-id_([0-9]*).html /phpBB/viewtopic.php?t=$2 [QSA]
RewriteRule ^/phpBB/forum-(.*)-id_([0-9]*).html /phpBB/viewforum.php?f=$2 [QSA]

QED, no? At least so far. I hacked this out in a hurry and I suspect the regex is not robust enough for all situations. Let me know if you have problems or improvements and I’ll update them here.

Update: I haven’t tried it yet, but a friend pointed me to this mod for phpBB.

This entry was posted in Uncategorized. Bookmark the permalink.