After being requested to write a tutorial a few times, I've finally found the time to create a mod_rewrite tutorial for vBulletin 3. What this tutorial will allow you to do is make your vBulletin forum search engine spiderable - for Google and all the others.
<< update >>
This hack is now obsolete! Please visit the new version:
http://www.daniweb.com/techtalkforums/thread35147.html
<< end update >>
Step One
My version of making vB 3 search engine spiderable is unique because I rename the actual URLs for each forum forum-name.html instead of just forum1.html, forum2.html, etc. Therefore, the first thing that needs to be done is to tell the vBulletin forum to parse the names of each forum and create an equivalent that can be used in a URL. This can be done by editing the file includes/functions_forumlist.php.
In that file, do a search for:
// do light bulb
$forum['statusicon'] = fetch_forum_lightbulb($forumid, $lastpostinfo, $forum);
Directly above that, add the following code:
// added by dani
$forum['url'] = strtolower(
str_replace(" ", "-",
str_replace("/", "-", $forum['title'])
)
);
// added by dani
By adding that code, we did a few things. The first thing that we did was parse the title of each forum and replace each instance of a space or the / character with a dash, and then convert all characters to lowercase. Therefore, a forum with a title "PHP/ASP" will be parsed as "php-asp" and a forum with a title "DaniWeb Community" will be parsed as "daniweb-community". The second thing that we did here was store the parsed version in the $forum variable, which we can now use in the vB3 templating system. In other words, the same way as we have always been able to use $forum in vB3 templates, we can now use $forum wherever we want as well.
Step Two
The next step is to update all of the vB3 templates. To do this, first go into the vB Admin control panel. Then, click the Style Manager, and select to Edit Templates for the style you'd like to use. The following templates will need to be edited:
Within the FORUMHOME template group:
forumhome_forumbit_level1_nopost
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">
forumhome_forumbit_level1_post
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">
forumhome_forumbit_level2_nopost
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">
forumhome_forumbit_level2_post
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">
forumhome_subforumbit_nopost
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">
forumhome_subforumbit_post
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">
All need to be replaced with the following:
<a href="$forum[url].html">
Something even simpler than making all those changes in multiple templates is to use the vBulletin replacement manager feature. In fact, all of the above steps would only require one replacement :)
Step Three
Now that we have made all the changes to point to our forums, we will need to make changes to point to the threads. Our thread URLs will be in the form of thread123.html.
A few changes will need to be made to the forumhome_lastpostby template, which we just finished editing above.
PART 1
<a href="showthread.php?$session[sessionurl]goto=newpost&t=$lastpostinfo[lastthreadid]" title="<phrase 1="$lastpostinfo[lastthread]">$vbphrase[go_first_unread_in_thread_x]</phrase>"><strong>$lastpostinfo[trimthread]</strong></a>
will need to be replaced with:
<a href="newpostinthread$lastpostinfo[lastthreadid].html" title="<phrase 1="$lastpostinfo[lastthread]">$vbphrase[go_first_unread_in_thread_x]</phrase>"><strong>$lastpostinfo[trimthread]</strong></a>
PART 2
<a href="showthread.php?$session[sessionurl]goto=lastpost&t=$lastpostinfo[lastthreadid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>
will need to be replaced with:
<a href="lastpostinthread$lastpostinfo[lastthreadid].html"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>
PART 3
<a href="showthread.php?$session[sessionurl]goto=lastpost&t=$lastpostinfo[lastthreadid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>
will need to be replaced with:
<a href="lastpostinthread$lastpostinfo[lastthreadid].html"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>
Step Four
We will also need to edit the threadbit template in four locations.
PART 1
<a href="showthread.php?$session[sessionurl]goto=lastpost&t=$thread[threadid]">
will need to be replaced with:
<a href="lastpostinthread$thread[threadid].html">
PART 2
<a href="showthread.php?$session[sessionurl]goto=newpost&t=$thread[threadid]">
will need to be replaced with:
<a href="newpostinthread$thread[threadid].html">
PART 3
<a href="showthread.php?$session[sessionurl]t=$thread[threadid]$thread[highlight]">
will need to be replaced with:
<a href="thread$thread[threadid].html">
PART 4
<a href="showthread.php?$session[sessionurl]t=$thread[threadid]&goto=lastpost$thread[highlight]">
will need to be replaced with:
<a href="lastpostinthread$thread[threadid].html">
Step Five
This next part is the most important, because it actually uses an .htaccess file to do the mod_rewrite. I am assuming that you have an Apache web server with mod_rewrite enabled.
Create a file called .htaccess and upload it to the root of your forum (the same directory as your forumdisplay.php and showthread.php files). Edit this file to include the following:
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^thread([0-9]+).html$ showthread.php?t=$1 [L]
RewriteRule ^lastpostinthread([0-9]+).html$ showthread.php?goto=lastpost&t=$1 [L]
RewriteRule ^newpostinthread([0-9]+).html$ showthread.php?goto=newpost&t=$1 [L]
RewriteRule ^forum([0-9]+).html$ forumdisplay.php?f=$1 [L]
Step Six
However, we also need to add more to this .htaccess file => one line for each and every forum we have. Remember how above we told vBulletin to parse our forum titles and convert them to forum URLs? Well, we have to unfortunately do this manually now for each forum we have. Follow the example below:
RewriteRule ^forum-one.html$ forumdisplay.php?f=1 [L]
RewriteRule ^forum-two.html$ forumdisplay.php?f=2 [L]
RewriteRule ^forum-three.html$ forumdisplay.php?f=3 [L]
A RewriteRule line should be created for each forum you have, and should always remain uptodate (should you edit/remove forums). Keep in mind that all spaces in a forum name should be replaced with dashes and all forward-slashes in a forum name should be replaced with dashes as well.
The reason why we have to do it this way is because suppose we have a forum called "DaniWeb PHP". It would be really great if we could create a custom php script called forumjump.php which would allow us to do forumjump.php?f=daniweb-php. The php file would then convert daniweb-php to "DaniWeb PHP". It would then search the MySQL database for a forum whose title is "DaniWeb PHP". It would then find the corresponding forum number. And then redirect the user to forumdisplay.php?f=XX. However, the overhead for doing that would be ridiculous. Every single time any user on your forum would visit a new forum or thread page, they would first need to have a php page do a database lookup and then a redirect.
To eliminate that overhead, we create a new entry in the .htaccess file for each forum. This way, we let Apache handle the forum name to forum ID number conversion. While it's true that mod_rewrite does have a lot of overhead, I don't think it would be as bad as if were done the other way.
If these last two paragraphs went over your head, don't worry about it. It was just a bit of a sidetracked explanation of why I have chosen to do things the way I have.
Step Seven
There are some more changes that need to be made, and these are in editing the navbits on the forumdisplay and showthread pages. We start by editing the navbar template within the Navigation / Breadcrumb templates group.
There are two instances of the following in that template:
<a href="$vboptions[forumhome].php?$session[sessionurl]" accesskey="1">
Both instances should be replaced with:
<a href="./" accesskey="1">
Step Eight
To finish editing the navbit breadcrumb, we must edit the .php files directly. To do this, we edit both the forumdisplay.php as well as the showthread.php files:
Do a search in forumdisplay.php for:
// draw nav bar
and do a search in showthread.php for
// draw navbar
A few lines below those lines, in both files, you will see a loop that looks somewhat like
foreach ($parentlist AS $forumID) {...}
Replace that loop with the following:
foreach ($parentlist AS $forumID)
{
$forumTitle = $forumcache["$forumID"]['title'];
$navbits["forum$forumID.html"] = $forumTitle; // edited by dani
}
Unfortunately, what we're doing here, is making the forums in the breadcrumb point to forum1.html and forum2.html. Due to reasons that are beyond the scope of this tutorial, it is impossible for us to use forum-name.html here. Basically, the way that vBulletin uses recursion to generate the breadcrumb, the forum ID numbers must be in the URL for it to work correctly. If you did use $navbits["forum$url.html"] instead of $navbits["forum$forumID.html"] then vBulletin would create all navbar breadcrumbs assuming that the forum you're currently browsing is a top-level forum. In other words, it is not a subforum of anyone. The way I figure it, at least forumXX.html is better than forumdisplay.php?f=XX.
Step Nine
Take a deep sigh of relief! You're done :) Now comes the hard part. You'll have to wait for google to spider your new clean URLs. ;)