ncyoung.com

You are here: Top->My Software->b2 hacks



b2 Hack: when viewing a single post, post title becomes page title

I had heard that Google pays particular attention to page titles when ranking pages in search results, so a couple weeks ago I modified b2 to use the post title as the page title when viewing one post at a time.

Sure enough, I'm getting a lot more google hits than before. Here's how it was done:

1. Open your blog template

2. Find the line that says:

include ("blog.header.php");

3. After that line, put the following:

if ($p){
//find title of the listing.
$sql = "SELECT * FROM $tableposts WHERE ID = $p";
//print $sql;
$tHandle = mysql_query($sql);
$tRow = mysql_fetch_assoc($tHandle);
$title = $tRow["title"];
}
else {
$title = "Default Title Here";
}


5. You can now use $title as the title in your HTMl with the following PHP tag:
<?=$title?>

b2 hack - URL rewrites make permalink anchors look static

I've been pondering how to make my weblog more friendly to search engines. This is part 1 of that process.

Older search engines did not index any URL with "get" method encoding (e.g. the ?p=85&c=1 URLs b2 uses for permalinks and comments). And I've been told even Google downgrades such links somehow, though I don't know that personally.

Anyway, I came up with a fairly simple way to make permalinks use a different format. This uses apache's rewrite rules from an htaccess file. Many but not all servers allow rewrite rules in .htaccess files, so that would be something to check before starting or when you run into trouble. (Note: all this works from apache config files as well, for those of you who maintain your own servers)

If you have trouble, feel free to drop me a line. Many of the things I outline here may vary slightly at your server, and I'm glad to help out.

All that said, here's how you do the trick.

1. Decide what path to use for permalinks. In this example and in my website I use the /permaLink directory. the only requirement is that it's not used for anything else on your site.

2. Create or modify the .htaccess file to do the rewrite. This is done in two lines:

RewriteEngine On
RewriteRule permaLink/([0-9]*) /index.php?p=$1&c=1

index.php is the address of my weblog. If yours has a different filename, substitute that in.

3. Upload and test the .htaccess file. On my setup, http://ncyoung.com/permaLink/85 returns the same info as http://ncyoung/index.php?p=85&c=1. Both URLs continue to work.

4. Once you have that working, you can change the permalink format in your weblog. Rather than re-write permalink_single() or the other built in b2 template functions, I commented that out in my page and used instead:

<A HREF="/permaLink/<?=the_ID()?>">permalink</A>

5. Make all the links in your template page absolute so that they don't break under the new path. Don't forget your stylesheet links.

If you have lots of hyperlinks and you want to leave them relative, you can use the following line in your .htaccess file:

RedirectMatch permanent permaLink/(([^0-9]).*) http://yourdomain.com/$1

That should do it! Again, email me if you have trouble!

update: download the new version of my refererLib.php, updated to work with this hack.

Referers (linkback) in b2.

I fixed my b2 blog to show referring pages from the permalink or comments view of a weblog entry.



Here are instructions for making this modification:



  • 1. Download the refererlog code from:

    http://ncyoung.com/code/zip/refererLib.zip

    More info available at:

    http://ncyoung.com/code.php

  • 2. Edit refererLib.php, placing your database connection variables in the connect statements at the top of the file. If you want the library to ignore internal links (i.e. those from one page to another within your own site) you must add your domain to the "ignore" array. If there are other sites whose referrals you'd like ignored, you may add them to the "ignore" array as well.

  • 3. Place the library in your b2 directory.

  • 4. visit http://path_to_b2/refererLib.php?createTable=1 to create the database table to hold referers.

  • 5. At the top of your main blog template, you should see a statement like:

    include ("blog.header.php");

    Right after this, add a statement:

    include ("refererLib.php");

  • 6. Again in your main blog template but much further down: Right before

    the line that says:

    < ?php include ("b2comments.php"); ?>

    insert the following code:



    ---------------------------code start

    < ?php

    if ($p && $c){

      logReferer();

      $list = refererList(20);

      if ($list){

        print "<br>Pages that link to this one:";

        foreach ($list as $link){

          print " < - $link<BR>";

        }

        print "<br>";

      }

    }

    ?>

    ---------------------------code end



  • 7. That's it





Update (7/22/02)

This modification posted at Adam Walker's site