Wednesday, July 19, 2006

mod_rewrite (killing me softly with its code)

Jeez Louise! I've been working on a set of dynamically generated pages for a business directory on the Chamber of Commerce site and while I understand (and love!) them, I've read that maybe the Search Engine gods don't.

I don't want to take any chances that these page won't be indexed, and it will eventually produce a page for each member that I'd like for them to be able to use i.e. www.chamber.com/members/business_name.

Enter the mod_rewrite...On one forum I found some idiot who recommended a handy-dandy tool to right the rule for you to use in .htaccess. I wasted all afternoon yesterday on that stupid, stupid tool. BTW, said idiot touts himself as a web marketer extraordinaire on his own site. If that's true, then I underestimate myself greatly!

Today, I found the REAL solution in another forum. The whole deal with mod_rewrite is that you use a SE friendly URL within your script, then use the RewriteRule to write the dynamically generated URL so that the script works. Thank you, Birdman!!

Here's the PHP code:
<?php
$sql = "select name from TABLE";
$r=mysql_query($sql);
while ($row=mysql_fetch_array($r, MYSQL_NUM))
{
$name=$row[0];
echo "<p><b><a href='members/$name' title='$name'>$name</a></b></p>";
}
?>

And the .htaccess code:
RewriteEngine on
RewriteRule ^members/(.*)$ members.php?name=$1 [L]


Then, in members.php I can use this code to display that member's page:
<?php
$sql = "select * from members where name={$_GET['name']}";
?>


The browser and SE will see: www.chamber.com/members/MEMBERNAME instead of www.chamber.com/members.php?name=MEMBERNAME and the member actually has a URL they can use on biz cards etc!

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home