I figured it was time that a revised edition of this tutorial be written up. So, I asked Dani if it was ok and well..... here it is :D

This revised edition includes how to make your forumhome, forumdisplay, showthread and who's online sections all show search engine friendly urls as well as in the navbar. It includes the id & name in the url of forums and threads.

Please take care when performing each step, making sure you perform the correct action - adding code ABOVE or BELOW other lines or REPLACING code.

Step 1 - File Edits
This step covers all files that need to be edited. Step 2 covers the template edits.

forumdisplay.php

Find:

foreach ($parentlist AS $forumID)
{
	$forumTitle = $forumcache["$forumID"]['title'];
	$navbits["forumdisplay.php?$session[sessionurl]f=$forumID"] = $forumTitle;
}

Replace with:

// ################ MOD_REWRITE HACK ######################
foreach ($parentlist AS $forumID) 
{
	$forumTitle = $forumcache["$forumID"]['title'];
	$forumURL = strtolower(str_replace(" ", "-",str_replace("/", "-", $forumTitle)));	 
	$navbits["f$forumID-$forumURL.html"] = $forumTitle;
}
// ################ MOD_REWRITE HACK ######################

Find:

$thread = process_thread_array($thread, $lastread, $foruminfo['allowicons']);

Below that add:

// ################ MOD_REWRITE HACK ######################
$thread['url'] = strtolower(str_replace(" ", "-", $thread['threadtitle']));
$thread['url'] = ereg_replace("[\/!£$%^&*«»,.;:'#@~{}|=+`¬<>?]+", "", $thread['url']);
// ################ MOD_REWRITE HACK ######################

showthread.php

Find:

foreach ($parentlist AS $forumID)
{
	$forumTitle = $forumcache["$forumID"]['title'];
	$navbits["forumdisplay.php?$session[sessionurl]f=$forumID"] = $forumTitle;
}

Replace with:

// ################ MOD_REWRITE HACK ######################
foreach ($parentlist AS $forumID)
{
	$forumTitle = $forumcache["$forumID"]['title'];
	$forumURL = strtolower(str_replace(" ", "-",str_replace("/", "-", $forumTitle)));
	$navbits["f$forumID-$forumURL.html"] = $forumTitle;
}
// ################ MOD_REWRITE HACK ######################

search.php

Find:

$thread = process_thread_array($thread, $lastread["$thread[forumid]"]);

Below that add:

// ################ MOD_REWRITE HACK ######################
$thread['url'] = strtolower(str_replace(" ", "-", $thread['threadtitle']));
$thread['url'] = ereg_replace("[\/!£$%^&*«»,.;:'#@~{}|=+`¬<>?]+", "", $thread['url']);
// ################ MOD_REWRITE HACK ######################

includes/functions_forumlist.php

Find:

// do light bulb
$forum['statusicon'] = fetch_forum_lightbulb($forumid, $lastpostinfo, $forum);

Above that add:

// ################ MOD_REWRITE HACK ######################
$forum['url'] = strtolower(str_replace(" ", "-",str_replace("/", "-", $forum['title'])));
// ################ MOD_REWRITE HACK ######################

Find:

$forumperms = $bbuserinfo['forumpermissions']["$forumid"];

Above that add:

// ################ MOD_REWRITE HACK ######################
$forum['url'] = strtolower(str_replace(" ", "-",str_replace("/", "-", $forum['title'])));
// ################ MOD_REWRITE HACK ######################

includes/functions_online.php

Find:

// ###################### Start showonline #######################

Above that add:

// ###################### MOD_REWRITE HACK #################
function mod_rewrite_title($title)
{
	$rewritten = strtolower(str_replace(" ", "-",str_replace("/", "-", $title)));
	return ereg_replace("[\/!£$%^&*«»,.;:'#@~{}|=+`¬<>?]+", "", $rewritten);
}
// ###################### MOD_REWRITE HACK #################

Find all instances of:

<a href=\"showthread.php?$session[sessionurl]t=$threadid\" title=\"$threadpreview\">$threadtitle</a>

And replace with:

<a href=\"t$threadid-" .mod_rewrite_title($threadtitle). ".html\" title=\"$threadpreview\">$threadtitle</a>

Find all instances of:

<a href=\"forumdisplay.php?$session[sessionurl]f=$forumid\">$forumtitle</a>

And replace with:

<a href=\"f$forumid-" .mod_rewrite_title($forumtitle). ".html\">$forumtitle</a>

Find:

convert_short_varnames($values);
$userinfo['values'] = $values;

Below that add:

// #################### MOD_REWRITE HACK #################
	if (eregi("t([0-9]+)-(.*).html$", $filename, $tmatch)) {
		$filename = 'showthread.php';
		$values['threadid'] = $tmatch[1];
	}
 
	if (eregi("f([0-9]+)-(.*).html$", $filename, $fmatch)) {
		$filename = 'forumdisplay.php';
		$values['forumid'] = $fmatch[1];
	}
// #################### MOD_REWRITE HACK #################

Step 2 - Template Edits
This step covers all the template edits needed. Step 3 covers the .htaccess file needed.

Within the Forum Home Templates group the following templates all require the modification below.

forumhome_forumbit_level1_nopost
forumhome_forumbit_level1_post
forumhome_forumbit_level2_nopost
forumhome_forumbit_level2_post
forumhome_subforumbit_nopost
forumhome_subforumbit_post

In each of the templates above find:

<a href="forumdisplay.php?$session[sessionurl]f=$forum[forumid]">

And replace with:

<a href="f$forum[forumid]-$forum[url].html">$forum[title]</a>

In the forumhome_lastpostby template find:

<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>

And Replace 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>

Find:

<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>

And replace 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>

Find:

<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>

And replace 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>

In the Threadbit Templates group, the threadbit template requires the following modifications.

Find:

<a href="showthread.php?$session[sessionurl]goto=lastpost&amp;t=$thread[threadid]">

And replace with:

<a href="lastpostinthread$thread[threadid].html">

Find:

<a href="showthread.php?$session[sessionurl]goto=newpost&amp;t=$thread[threadid]">

And replace with:

<a href="newpostinthread$thread[threadid].html">

Find:

<a href="showthread.php?$session[sessionurl]t=$thread[threadid]$thread[highlight]">

And replace with:

<a href="t$thread[threadid]-$thread[url].html">

Find:

<a href="showthread.php?$session[sessionurl]goto=lastpost&amp;t=$thread[threadid]">

And replace with:

<a href="lastpostinthread[threadid].html">

Step 3 -.htaccess file

Open notepad or your usual text editor and copy and paste the following into a new file.

RewriteEngine on
Options +FollowSymLinks
RewriteRule ^t([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 ^f([0-9]+).html$ forumdisplay.php?f=$1 [L]

Save this file as .htaccess and upload it to your forum's root directory.


And that's it!

Now just sit back and wait for the search engines to spider your new urls :o

If I missed any steps (god forbid) or there's an error in any of the steps, please let me know and I'll update it.

There are two things that need mentioning:

1. In the includes/functions_forumlist.php file where I mention to find

$forumperms = $bbuserinfo['forumpermissions']["$forumid"];

and and above it add

// ################ MOD_REWRITE HACK ######################
 $forum['url'] = strtolower(str_replace(" ", "-",str_replace("/", "-", $forum['title'])));
 // ################ MOD_REWRITE HACK ######################

There are 3 instances of the line. It's the last instance (around line 530) you place the code above.

2. In the includes/functions_online.php file where I mention replacing all instances of

<a href="showthread.php?$session[sessionurl]t=$threadid\" title=\"$threadpreview\">$threadtitle</a>

with

<a href="t$threadid-" .mod_rewrite_title($threadtitle). ".html\" title=\"$threadpreview\">$threadtitle</a>

and replacing

<a href="forumdisplay.php?$session[sessionurl]f=$forumid\">$forumtitle</a>

with

<a href="f$forumid-" .mod_rewrite_title($forumtitle). ".html\">$forumtitle</a>

If your code has backslashes ( \ ), you need to keep them in the code.

Great work KeithMcL, thanks for doing this!

/me heads off to change the link in my Sig to point to this tute :)

Great job Dani for this resource :)

No problem. Hope it works ok for everyone :)

One suggestion: is there any reason why we shouldn't have the mod_rewrite_title() function defined only in includes/functions.php, and call that to rewrite every title in the hack? that way the second element of the function (the

ereg_replace("[\/!£$%^&*«»,.;:'#@~{}|=+`¬<>?]+", "", $rewritten);

) could be applied across the board, ensureing that all links across the site will match, making the SEO'ness better (making sure there is only one "page" per page of content).

I have a question about the step below


includes/functions_online.php
Find all instances of:

<a href=\"showthread.php?$session[sessionurl]t=$threadid\" title=\"$threadpreview\">$threadtitle</a>

And replace with:

<a href=\"t$threadid-" .mod_rewrite_title($threadtitle). ".html\" title=\"$threadpreview\">$threadtitle</a>

In my includes/functions_online.php I have

<a href=\"showthread.php?$session[sessionurl]t=$threadid\" title=\"$threadpreview\">$threadtitle</a>

but the above shows

<a href="showthread.php?$session[sessionurl]t=$threadid\" title=\"$threadpreview\">$threadtitle</a>

what do I do? it appears that when you use the code tags here to show code it takes out the a href=\ and makes it a href=

Am I correct with this as it states in one of the above posts (post #2)

If your code has backslashes ( \ ), you need to keep them in the code.

Help pls

Very interesting, I'd love to see this on our boards :)

I tested this on our testing forum, but in contrast to dani's hack it doesn't seem to work: http://forum.mbrd.de

Any ideas what I might have overseen?

Thanks,
-Tom

I have a question about the step below


In my includes/functions_online.php I have

<a href=\"showthread.php?$session[sessionurl]t=$threadid\" title=\"$threadpreview\">$threadtitle</a>

but the above shows

<a href="showthread.php?$session[sessionurl]t=$threadid\" title=\"$threadpreview\">$threadtitle</a>

what do I do? it appears that when you use the code tags here to show code it takes out the a href=\ and makes it a href=

Am I correct with this as it states in one of the above posts (post #2)


Help pls

I *think* this is a typo man...

This is mighty cool stuff, and I seem to have it all working fine on NZB.

After 2 days, my Adsense ads are a lot more targetted. I don't know if it's due to this hacking, but I'll assume it is and add my thanks. :)

One suggestion: Maybe putting it all up as a .txt file would prove helpful...or something that doesn't strip the backslashes and "&amp" bits.

One suggestion: Maybe putting it all up as a .txt file would prove helpful...or something that doesn't strip the backslashes and "&amp" bits.

That would be nice as that is what I had seen when I was trying to use some of this, I was having a very hard time when I was doing the searches, untill I noticed that the backslashes were stripped.... so I stopped.

Didn't want to go to far and something go wrong and not knowing where it happened would be a pain to fix.

Coll

@Nordinho: Exactly the same I experience. So mod_rewrite seems to work, but something is going wrong.

@KeithMcL: Could you please bare with us and try to push us in the right direction?

Thanks,
-Tom

@Nordinho: Exactly the same I experience. So mod_rewrite seems to work, but something is going wrong.

@KeithMcL: Could you please bare with us and try to push us in the right direction?

Thanks,
-Tom

Anyone who can help us??

Anyone who can help us??

Thomas P, I found us the fix!! :p

Keith used the wrong rewrite lines...your htaccess file should look like the one below...

RewriteEngine On
Options +FollowSymLinks
RewriteRule ^t([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 ^f([0-9]+)-(.*).html$ forumdisplay.php?forumid=$1 [L]

Hey guys. I fixed the backslash stripping problem. Sorry about that!

Hey guys. I fixed the backslash stripping problem. Sorry about that!

Thanks Dani, but it's still not quite right, in the functions/online bit anyway.

$userinfo['where'] = "<a href=\"t$threadid-" .mod_rewrite_title($threadtitle). ".html\" title=\"$threadpreview\">$threadtitle</a> ";

That line is what works.

hello all... I am thinking of adding vBadvanced CMPS and was wondering if any one is also using it with this Mod_rewrite or have any idea if it will work or not, or any problems that can happen.

Anyone noticed that using this or KeithMcL's hack will close out user who don't use cookies and need sessionhashes?

(?)

If it's not a silly question, how would the the file "external.php" need to be modded, so that RSS feed of the forums would show the HTML threads, rather than the dynamic php URLs?

In external.php,

echo "\t\t<link>$vboptions[bburl]/showthread.php?t=$thread[threadid]&amp;goto=newpost</link>\r\n";

would need to be replaced with

echo "\t\t<link>$vboptions[bburl]/newpostinthread$thread[threadid].html</link>\r\n";

and

echo "\t\t<guid isPermaLink=\"false\">$vboptions[bburl]/showthread.php?t=$thread[threadid]</guid>\r\n";

would need to be replaced with

echo "\t\t<guid isPermaLink=\"false\">$vboptions[bburl]/thread$thread[threadid].html</guid>\r\n";

Dani, as ever, you are a star. :)

Dani, just one small issue with external.php - when calling up syndicated content for individual vB3 sub-forums, you use something like:

external.php?forumids=38

to call up a feed of that subforums.

However, it doesn't seem to like this on the workaround to static HTML. I *presume* it's an issue to be corrected with .htaccess, but I'm not quite sure about how to go about it.

Would you possibly have any suggestion?

Alright im implementing this new revised on a new site of mine, and it doesnt really go smoothly, had to figure alot of things out myself :( Btw the htaccess code was wrong i thought, as the threads you clicking into will have the title in the filename, you need something like this

RewriteEngine on
Options +FollowSymLinks
RewriteRule ^t([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 ^f([0-9]+)-(.*).html$ forumdisplay.php?f=$1 [L]

Im having this error "Parse error: parse error, unexpected T_STRING in /home/musicp/public_html/forums/includes/functions_online.php on line 164" when i go to who online or try to view a profile. Im really quite new to PHP, any idea guys ?

Good catch on that one Nordinho: Dani: this (probably) should be edited into the original post of this thread...

Got a problem. The rewite works fine, except that I'm seeing duplicate forum names. Any idea what I need to edit to fix this?

www.clantt.com/vb/ is the url in question.

Thanks in advance. Nice work.

Excellent tutorial...nice job. Should be very handy..I might try this

I agree with ewebtalk great tutorial!

I just installed the first VB forum that I own yesterday (www.mysportsforum.com) and this tutorial is DA BOMB baby!

Thanks everybody!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.