The last post, we showed you how to add datafeeds to a site utilizing php and the MagpieRSS script. In this post, I am going to show you how to go one step further for search engine optimization using your .htaccess file and the str_replace command in php.

In the last post, I showed you how to use an rss feed from craigslist and incorporate it into your site. This time, I am going to show you how to take the same feed and rename all urls in the feed to urls that are more appropriate to your site.

I cannot be sure how this will react with wordpress because I have not tried it yet, however, I am going to give it a shot tomorrow and see what needs to be changed and will write a post about that.

In the last post, we pointed you to Boat Classifieds. Since the last post, I have taken all of the links from that by removing the $href statement.

Now if you have a custom php script or would like to run this with no style, feel free to copy this into wordpad and save as index.php or whatever you choose to rename it.

Okay, enough of the talk, here is the code:
Options -Indexes
RewriteEngine on
RewriteRule ^boat_classifieds/([0-9]+)-([a-zA-Z0-9?-]+) http://mobile.craigslist.org/boa/$1.html

Now to discuss what we are doing. This is telling your server to redirect all links that appear like this: www.yoursite.com/boat_classifieds/1-This_is_your_boat_ad.html to craigslist. The $1 tells the file that the first string of the url defined ([0-9]+) is the string that it will redirect to.

Now for the php code to actually rewrite the urls so that the urls listed actually appear to belong to your site.<?php
require_once('magpierss/rss_fetch.inc');
$url = "http://mobile.craigslist.org/boa/index.rss";
$rss = fetch_rss ( $url );
echo "<ul>";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$title1 = seo_str($title);
$href1 = str_replace("http://mobile.craigslist.org/boa/","",$href);
$href1 = str_replace(".html","",$href1);
$href = $site_url.'/boat_classifieds/'.$href1.'-'.$title1.'.html';
$description = $item['description'];
$description = str_replace("a href","a target='_blank' rel='nofollow' title='$title' href", $description);
echo"<li><a href=$href target='_blank' rel='follow' title='$title><h2>$title</h2>lt;/a></li> <p>$description</p>";
}
echo"</ul>";
?>

In this code, on line 3 we define the rss feed that we are going to show on our site. Lines 6-8 grab the information from the rss feed. On line 9 we define a function that will be useful for Search Engine Optimization by replacing all irregular characters with an underscore. In the rest of the code, we define the links, description, and then show them in html with the echo command. Specifically, lines 18-20 define the new url that we are going to redirect. On line 22 we redefine how the link opens so that it opens in a new window or tab and allows your original window open.

Preview the code here at Mobile Alabama Boat Classifieds.
You may copy this code now and test it, modify or etc, as you see fit.