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.

December 13th, 2004 at 12:13 pm
Hi Derek,
You have convinced me to use WordPress, It seems to be perfect for my needs. I’ll let you know how it goes. We are going to turn the DMA archives into a blog. Duh. Don’t know why I didn’t think of that earlier.
Doug
August 8th, 2005 at 7:55 pm
Hello,
i tried to install this hack but so far it hasnt worked… i found some minor errors with the code above but i think i managed to correct them… not sure if i did the right thing…
==============Index.php=============
$forum_name=$forum_data[$j][’forum_name’];
$view_forum_url=trim($forum_name);
$view_forum_url=’forum-’ .$view_forum_url;
$view_forum_url=str_replace('’,'-’,$view_forum_url);
$view_forum_url=str_replace(’/',’-',$view_forum_url);
$view_forum_url=(urlencode($view_forum_url).”-id_$forum_id”);
$view_forum_url .=’.html’;
$view_forum_url=append_sid($view_forum_url);
===============end index.php============
i added ‘U_VIEWFORUM’ => $view_forum_url) and commented the line as shown
===============viewforum.php==============
$view_topic_url=trim($topic_title);
$view_topic_url=’topic-’ . $view_topic_url;
$view_topic_url=str_replace(’ ‘,’-',$view_topic_url);
$view_topic_url=str_replace(’/',’-',$view_topic_url);
$view_topic_url = (urlencode($view_topic_url).”-id_$topic_id)”;
$view_topic_url .=’.html’;
$view_topic_url=append_sid($view_topic_url);
==========end of viewforum.php==========
============ .htaccess =============
RewriteEngine on
RewriteRule ^/sun/topic-(.*)-id_([0-9]*).html /sun/viewtopic.php?t=$2 [QSA]
RewriteRule ^/sun/forum-(.*)-id_([0-9]*).html /sun/viewforum.php?f=$2 [QSA]
RewriteCond %{HTTP_REFERER} !^http://sunsigns.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://sunsigns.org$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.sunsigns.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.sunsigns.org$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.sunsigns.org [R,NC]
============end of .htaccess==========
i cant figure out what i am doin wrong? maybe you can help?
http://www.netalways.com/sun
the real domain is www.sunsigns.org but dont wont to do stuff there till it work here. i am using phpBB 2.0.14. with eXtreme Styles and Morpheus.
Thanks in advance,
Abhishek
August 10th, 2005 at 9:59 am
Hi Abishek,
Sorry you’re having problems. It’s hard to diagnose just by looking at the code. The code looks correct to me, but these kinds of things can be maddening.
To debug it, the first thing I would do is check to see if the regex is working. To do that, try redirecting to a site you *know* works, like Yahoo or Google:
RewriteRule ^/sun/topic-(.*)-id_([0-9]*).html http://www.yahoo.com [QSA]
RewriteRule ^/sun/forum-(.*)-id_([0-9]*).html http://www.google.com [QSA]
With this setup, clicking on a topic takes you to Yahoo, and clicking on a forum link takes you to Google.
If you don’t end up at Yahoo or Google, that means the regex is not being triggered. If you *do* get redirected, that means the regex is working, but that the final redirect is not properly formed.
August 10th, 2005 at 11:31 pm
hi,
finally seemed to get it to work… i suspect it was the regex after all.
i got another question…
line >> $view_forum_url=’forum-’ .$view_forum_url;
is we want to replace ‘forum-’ with say ‘india-’ or maybe even ‘$forum_name’ .. would that be ok?
i tried it but it didnt work for me?
Thanks,
Abhishek
August 10th, 2005 at 11:57 pm
You can use ‘india’ instead of ‘forum’ just so long as you replace it in the regex. However, you can’t drop the constant (’india’ or ‘forum’) altogether and just use $forum_name because those constants are handles used to pair the ID of the forum/topic with the proper script (viewforum.php or viewtopic.php).
In other words, something like ‘india-’ . $forum_name . ‘-’ . etc should work with this rule:
RewriteRule ^/sun/india-(.*)-id_([0-9]*).html /sun/viewforum.php?f=$2 [QSA]
.. but $forum_name . ‘-’ without the ‘india’ prefix won’t work because the rewrite rule won’t know which script to send it to.