RSS Forums RSS

vBulletin mod_rewrite

Reply
Posts: 11,327
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 124
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Solution vBulletin mod_rewrite

  #1  
Aug 15th, 2004
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:
[php]
// do light bulb
$forum['statusicon'] = fetch_forum_lightbulb($forumid, $lastpostinfo, $forum);
[/php]

Directly above that, add the following code:
[php]
// added by dani
$forum['url'] = strtolower(
str_replace(" ", "-",
str_replace("/", "-", $forum['title'])
)
);
// added by dani
[/php]
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['url'] 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['forumid'] in vB3 templates, we can now use $forum['url'] 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[php]
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">[/php]
forumhome_forumbit_level1_post[php]
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">[/php]
forumhome_forumbit_level2_nopost[php]
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">[/php]
forumhome_forumbit_level2_post[php]
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">[/php]
forumhome_subforumbit_nopost[php]
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">[/php]
forumhome_subforumbit_post[php]
<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">[/php]
All need to be replaced with the following:

[php]<a href="$forum[url].html">[/php]

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
[php]<a href="showthread.php?$session[sessionurl]goto=newpost&amp;t=$lastpostinfo[lastthreadid]" title="<phrase 1="$lastpostinfo[lastthread]">$vbphrase[go_first_unread_in_thread_x]</phrase>"><strong>$lastpostinfo[trimthread]</strong></a>[/php]
will need to be replaced with:
[php]<a href="newpostinthread$lastpostinfo[lastthreadid].html" title="<phrase 1="$lastpostinfo[lastthread]">$vbphrase[go_first_unread_in_thread_x]</phrase>"><strong>$lastpostinfo[trimthread]</strong></a>[/php]

PART 2
[php]<a href="showthread.php?$session[sessionurl]goto=lastpost&amp;t=$lastpostinfo[lastthreadid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>[/php]
will need to be replaced with:
[php]<a href="lastpostinthread$lastpostinfo[lastthreadid].html"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>[/php]

PART 3
[php]<a href="showthread.php?$session[sessionurl]goto=lastpost&amp;t=$lastpostinfo[lastthreadid]"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>[/php]
will need to be replaced with:
[php]<a href="lastpostinthread$lastpostinfo[lastthreadid].html"><img class="inlineimg" src="$stylevar[imgdir_button]/lastpost.gif" alt="$vbphrase[go_to_last_post]" border="0" /></a>[/php]

Step Four

We will also need to edit the threadbit template in four locations.

PART 1
[php]<a href="showthread.php?$session[sessionurl]goto=lastpost&amp;t=$thread[threadid]">[/php]
will need to be replaced with:
[php]<a href="lastpostinthread$thread[threadid].html">[/php]

PART 2
[php]<a href="showthread.php?$session[sessionurl]goto=newpost&amp;t=$thread[threadid]">[/php]
will need to be replaced with:
[php]<a href="newpostinthread$thread[threadid].html">[/php]

PART 3
[php]<a href="showthread.php?$session[sessionurl]t=$thread[threadid]$thread[highlight]">[/php]
will need to be replaced with:
[php]<a href="thread$thread[threadid].html">[/php]

PART 4
[php]<a href="showthread.php?$session[sessionurl]t=$thread[threadid]&amp;goto=lastpost$thread[highlight]">[/php]
will need to be replaced with:
[php]<a href="lastpostinthread$thread[threadid].html">[/php]

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:

[php]
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]
[/php]

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:

[php]
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]
[/php]

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:
[php]<a href="$vboptions[forumhome].php?$session[sessionurl]" accesskey="1">[/php]
Both instances should be replaced with:
[php]<a href="./" accesskey="1">[/php]

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:
[php]// draw nav bar[/php]
and do a search in showthread.php for
[php]// draw navbar[/php]
A few lines below those lines, in both files, you will see a loop that looks somewhat like [php]foreach ($parentlist AS $forumID) {...}[/php]
Replace that loop with the following:
[php]foreach ($parentlist AS $forumID)
{
$forumTitle = $forumcache["$forumID"]['title'];
$navbits["forum$forumID.html"] = $forumTitle; // edited by dani
}[/php]

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.
Last edited by happygeek : Oct 28th, 2006 at 2:29 pm.
Dani the Computer Science Gal
AddThis Social Bookmark Button
Reply With Quote  
Posts: 40
Reputation: Ted S is an unknown quantity at this point 
Solved Threads: 0
Ted S Ted S is offline Offline
Light Poster

Re: vBulletin mod_rewrite

  #2  
Aug 15th, 2004
My site has well over 300 forums and subforums so the idea of making an entry for each one is no good...

Wouldn't it be easier just to give the forums numeric ids and use modrewrite to redirect based upon that information so you didn't have to keep updating htaccess?
Reply With Quote  
Posts: 11,327
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 124
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: vBulletin mod_rewrite

  #3  
Aug 15th, 2004
Yes, here at DaniWeb we have over 90 forums, and therefore we use numeric IDs, which eliminates creating a custom line for each forum in the .htaccess file. However, named forum URLs (especially ones which are keyword-rich) are very good for SEO purposes.

Regardless, the following are the differences should one want to go about using numeric forum IDs:

Step One => not needed

Step Two => All instances should be replaced with:
[php]<a href="forum$forum['forumid'].html">[/php]
instead of with
[php]<a href="$forum['url'].html">[/php]

Step Six => not needed

Everything else would remain the same as the above tutorial.
Dani the Computer Science Gal
Reply With Quote  
Posts: 40
Reputation: Ted S is an unknown quantity at this point 
Solved Threads: 0
Ted S Ted S is offline Offline
Light Poster

Re: vBulletin mod_rewrite

  #4  
Aug 15th, 2004
I really liked the idea of the titles in the url so I played aroung with modrewite and came up with this:

[php]RewriteRule ^/f([0-9]+)-(.*)\.html$ /forumdisplay.php?forumid=$1 [L][/php]

This tells modrewrite to forward any url fXID-XTEXT-.html to the forumdisplay.php?forumid=$XID url, this way you can have any text you want but since the ID is used in the URL, it is possible to use one rewrite rule for every forum.

To go along with this, you need to modify your forumbit templates to use the link
[php]<a href="f$forum[forumid]-$forum[url].html">[/php]

No files need to be changed as far as I can recall.

So far I am testing this on a template that my users don't access so please confirm this works for you before putting it on a live site.

Thanks for the wonderful hack, I had limited spidering in the past and it worked wonders so I can only expect even better results from this deep spidering system.
Reply With Quote  
Posts: 11,327
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 124
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: vBulletin mod_rewrite

  #5  
Aug 15th, 2004
Thanks so much Ted for your addition. At first glance, it looks like it should work fine. To everyone else out there, what Ted is proposing is that the URL look like this, given that forum #2 has the name DaniWeb Community: f2-daniweb-community.html - The forum system parses the forum id as 2 (hence no need for a loooong .htaccess) and the URL is still keyword rich.
Dani the Computer Science Gal
Reply With Quote  
Posts: 6
Reputation: ToOnZ is an unknown quantity at this point 
Solved Threads: 0
ToOnZ ToOnZ is offline Offline
Newbie Poster

Re: vBulletin mod_rewrite

  #6  
Aug 15th, 2004
Thanks for this fantastic hack, cscgal

I suppose this will work for VB3.0.3 and would you provide help if vBulletin releases a new version and this hack needs to be updated ?
Reply With Quote  
Posts: 11,327
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 124
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: vBulletin mod_rewrite

  #7  
Aug 15th, 2004
This hack should work for all gold versions of vB 3. I'll offer as much assistance as I possibly can. However, I can't guarantee support for not-yet-released versions of vB as just last week my vBulletin license to the members area expired But there shouldn't be any problems anyways - and if they are, just give any of the great guys over here a hollar.
Dani the Computer Science Gal
Reply With Quote  
Posts: 40
Reputation: Ted S is an unknown quantity at this point 
Solved Threads: 0
Ted S Ted S is offline Offline
Light Poster

Re: vBulletin mod_rewrite

  #8  
Aug 15th, 2004
Since I can't seem to edit my post, I have a little update... if you use my code please change [L] to [R] in your htaccess file which redirect to a url instead of naming the page to that url. This is less optimal for your SEO placement but it won't harm things like the quick style chooser which rely on a dynamic url existing.
Reply With Quote  
Posts: 6
Reputation: ToOnZ is an unknown quantity at this point 
Solved Threads: 0
ToOnZ ToOnZ is offline Offline
Newbie Poster

Re: vBulletin mod_rewrite

  #9  
Aug 15th, 2004
Thanks cscgal

Ted, if i didnt allow style changes then i should continue to use [L] ?
Reply With Quote  
Posts: 11,327
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 124
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: vBulletin mod_rewrite

  #10  
Aug 15th, 2004
Yes, [L] is much better than [R] for SEO reasons. By using [R], the user is actually physically redirected to the php? URL. I don't enable style changes, and have never ran into any problems with any other forum feature.

Ted, I will look into modifying the code that generates the style dropdown so that everyone can use [L].
Dani the Computer Science Gal
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the PHP Forum
Views: 83052 | Replies: 238 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:29 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC