Search Engine Friendly URL For Your Detail Pages

April 5th, 2008 | by Walter |

This is an older mod, but one that I wanted to review because of it’s benefits for SEO and for directory uniqueness.

This mod was written and originally posted by Boby, one of the phpLD developers.

I recommend this mod for a new directory or a slightly new directory with few pages indexed by search engines as it will change the URLs to the details page.

Here are the mod instructions, remember, you can only use this on phpLD versions 3+

STEP-1
First, you need to add a function to your /include/functions.php file:

Code:
function makeLinkTitleURL($titleURL, $excludeID=0)
{
global $db, $tables;

//Remove entities
$titleURL = strtr ($titleURL, array_flip (get_html_translation_table (HTML_SPECIALCHARS, ENT_QUOTES)) + array (’'’ => ‘\’‘, ‘ ‘ => ”));

//Strip PHP and HTML tags
$titleURL = strip_tags ($titleURL);

//Strip special chars
//Remove chars except letters, numbers, “_”, “-” and “+”
$titleURL = preg_replace (’#[^+_-\w\d]#’, ‘-’, $titleURL);

//Remove multiple occurrences of some replacement chars
$titleURL = preg_replace (array (’#[\_]+#’, ‘#[-]+#’, ‘#[+]+#’), array (’_', ‘-’, ‘+’), $titleURL);

//Strip all kind of white-space chars
$search = array ( “\s”,   //Space
“\n”,   //*NIX
“\r”,   //Mac
“\r\n”, //Windows
“\t”,   //Tab
“\x0B”, //Vertical Tab
“\0″    //NULL BYTE
);
$titleURL = str_replace ($search, ”, $titleURL);

//Trim replacement chars from end& beginning
$titleURL = trim (trim (trim ($titleURL, ‘_’), ‘-’), ‘+’);

//Make lowercase
$titleURL = strtolower ($titleURL);

//Make sure title URL is unique
$count = $db->GetOne(”SELECT COUNT(*) as `count` FROM `{$tables[’link’][’name’]}` WHERE `TITLE_URL` LIKE ” . $db->qstr($titleURL) . ($excludeID > 0 ? ” AND `ID` <> “.$db->qstr($excludeID) : “”));
if ($count > 0)
{
$titleURL .= ‘-’;

$i = 0;
do
{
$i++;

$titleURL .= $i;
$count = $db->GetOne(”SELECT COUNT(*) as `count` FROM `{$tables[’link’][’name’]}` WHERE `TITLE_URL` LIKE ” . $db->qstr($titleURL) . ($excludeID > 0 ? ” AND `ID` <> “.$db->qstr($excludeID) : “”));
} while ($count > 0);
}

return $titleURL;
}

Based on the link’s title, it will create a unique title URL by stripping all special characters, spaces, etc. If the title URL is not unique it will append a number and try again until it is unique. This is very important to have a unique title URL for each link.

STEP-2
The next step is to edit your .htaccess file, make sure you add the highlighted code to the same location as here described:

Code:
   ##Pagination Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*)page-(\d+)\.htm[l]?(.*)$  $1/?p=$2 [PT,NC]

##Custom detail page Rewrite
RewriteRule %{REQUEST_URI} !page-(\d+)\.htm[l]?$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*)[/](.*)\.htm[l]?(.*)$ detail.php [QSA,NC,L]

##Category redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [QSA,L]

STEP-3
Edit now your link.tpl file, located in /templates/YOUR_TPL/. You’ll have the following code, or similar, where you create the links for the detail pages:

Code:
<a href="{if !$smarty.const.ENABLE_REWRITE}
{$smarty.const.DOC_ROOT}/detail.php?id={$link.ID}
{else}
{$smarty.const.DOC_ROOT}/detail/link-{$link.ID}.html{/if}” title=”{l}Read more about{/l}: {$link.TITLE|escape|trim}”>{l}Read more{/l}</a>

Replace with:

Code:
<a href="{if !$smarty.const.ENABLE_REWRITE}
{$smarty.const.DOC_ROOT}/detail.php?id={$link.ID}
{else}
{$smarty.const.DOC_ROOT}/{$link.CATEGORY_URL|escape}{$link.TITLE_URL|escape}.html{/if}” title=”{l}Read more about{/l}: {$link.TITLE|escape|trim}”>{l}Read more{/l}</a>

Or if you want to use only the deepest category in URL use:

Code:
<a href="{if !$smarty.const.ENABLE_REWRITE}
{$smarty.const.DOC_ROOT}/detail.php?id={$link.ID}
{else}
{$smarty.const.DOC_ROOT}/{$link.CATEGORY_URL|regex_replace:”#(.*[\/])?(.*)[\/]$#i”:’\2/’}{$link.TITLE_URL|escape}.html{/if}” title=”{l}Read more about{/l}: {$link.TITLE|escape|trim}”>{l}Read more{/l}</a>

STEP-4
Now you’ll need to edit detail.php to determine the link ID based on the new rewrite rules. Find on top of the file:

PHP Code:
if (ENABLE_REWRITE == 1)
{
preg_match (‘#(.*)link(_|-)(\d+)\.htm[l]?$#i’, request_uri(), $matches);

$id = (!empty ($matches[3]) ? intval ($matches[3]) : 0);
}

Replace with:

PHP Code:
if (ENABLE_REWRITE == 1)
{
preg_match (‘#(.*)[\/](.*)\.htm[l]?$#i’, request_uri(), $matches);

if (!empty (

$matches[2]))
$id = $db->GetOne(“SELECT `ID` FROM `{$tables[’link’][’name’]}` WHERE `TITLE_URL` = “.$db->qstr($matches[2]));
else
$id = 0;
}

STEP-5
We need to create the title URL for new submitted links, so edit submit.php, look for the following code and edit as highlighted:

Code:
if ($action == 'edit')
{
$data[’TITLE_URL’] = makeLinkTitleURL($data[’TITLE’], $link_id);

$OldLinkType = $db->GetOne(”SELECT `LINK_TYPE` FROM `{$tables[’link’][’name’]}` WHERE `ID` = “.$db->qstr($link_id));
if ($OldLinkType == $data[’LINK_TYPE’])
$MoveToPayment    = false;

$data[’LINK_ID’]     = $payment_id = $link_id;
$submit_notification = $db->Replace($tables[’link_review’][’name’], $data, ‘ID’, true);
$RegularLink_notif   = false;
}
else
{
$data[’TITLE_URL’] = makeLinkTitleURL($data[’TITLE’]);

$payment_id = $data[’ID’];
unset ($data[’MARK_REMOVE’]);
$submit_notification = $db->Replace($tables[’link’][’name’], $data, ‘ID’, true);
}

STEP-6
Admins add/edit links too, so edit now /admin/dir_links_edit.php and /admin/dir_review_links_edit.php and look for (both files need the same edit):

PHP Code:
if (db_replace(‘link’, $data, ‘ID’) > 0)

Right before that, add this:

PHP Code:
$data[‘TITLE_URL’] = makeLinkTitleURL($data[‘TITLE’], $data[‘ID’]);

STEP-7
Before you can use all the above, the PLD_LINK and PLD_LINK_REVIEW database tables need an extra field where to store the title URL. To simply create those fields, use the following SQL query in phpMyAdmin:

Code:
ALTER TABLE `PLD_LINK` ADD `TITLE_URL` VARCHAR(255) NULL AFTER `TITLE`;
ALTER TABLE `PLD_LINK_REVIEW` ADD `TITLE_URL` VARCHAR(255) NULL AFTER `TITLE`;

STEP-8
The last step. In spider.php search for:

PHP Code:
$saveStatus = $db->Replace($tables[‘link’][‘name’], $data, ‘ID’, true);

Right before add:

PHP Code:
$data[‘TITLE_URL’] = makeLinkTitleURL($data[‘TITLE’]);

Here is a little script that will create the title URLs for your links automatically and also create the fields in the DB tables (so you can skip the above SQL queries). Download the attachment file maketitleurls.txt and upload it to your server as maketitleurls.php in the root folder of your phpLD installation. Then execute the script by running:

Code:
http://domain.com/maketitleurls.php

Depending on how many links you have, it could take a while to generate.
Run it once, then remove the file! (using it again later could change the detail link page URLs).

For more information on this Mod and for the full thread, visit: http://www.phplinkdirectory.com/forum/showthread.php?t=19138

Permalink: http://www.waltervictor.com/blog/php-link-directory/search-engine-friendly-url-for-your-detail-pages.html
Trackback: http://www.waltervictor.com/blog/php-link-directory/search-engine-friendly-url-for-your-detail-pages.html/trackback/


You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a comment or trackback from your own site.


Post a Comment