943,423 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 110889
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 15th, 2004
1

vBulletin mod_rewrite

Expand Post »
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 3:29 pm.
Similar Threads
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
Ted S is offline Offline
40 posts
since Aug 2004
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ted S is offline Offline
40 posts
since Aug 2004
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ToOnZ is offline Offline
6 posts
since Aug 2004
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ted S is offline Offline
40 posts
since Aug 2004
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

Thanks cscgal

Ted, if i didnt allow style changes then i should continue to use [L] ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ToOnZ is offline Offline
6 posts
since Aug 2004
Aug 15th, 2004
0

Re: vBulletin mod_rewrite

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].
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: DNS subdomain wildcard
Next Thread in PHP Forum Timeline: Perplexed Newbie can't find syntax errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC