If you have done a PHP based website, chances are you have dynamic content called by variables and so on.
As many people use a main page (index.php) and use code within that page to dictate what content is displayed, either with if or switches or whatever.
Thing is, Google and other search engines dont like these dynamic pages and doesnt index them as they would static html pages.
Following the example of daniweb, i figured it wouldnt take much to fool these engines into thinking the pages were static. So, without further a'do, Here's how to do it...
(assumes you have php installed and use 1 root page (index.php) to navigate and display the content of the site)
Note: Does not require Apache like other mod-rewrite methods. Can be used on Windows servers(IIS) with PHP installed.
Search Engine compatible links for dynamic PHP sites
<?php
/* This first bit checks to see if $PATH_INFO is seton your php install/server
which is generally is on both apache and IIS.
Then converts the /path/to/places/links to variables path=to places=links etc. */
if(isset($PATH_INFO)) {
$vardata = explode('/', $PATH_INFO);
$num_param = count($vardata);
if($num_param % 2 == 0) {
$vardata[] = '';
$num_param++;
}
for($i = 1; $i < $num_param; $i += 2) {
$$vardata[$i] = $vardata[$i+1];
}
}
/* Now that we have a URL handler, we just need to have the links on the pages contents
to be converted to the same style as above. e.g. http://www.domain.com/index.php/page/links
To do this we use the following functions to do the converting, and simply pass the content page
or html template to it for conversion. */
function linkMorph($link) {
$link = explode ("?", $link); if ($link[1] != "") {
$ndata = explode("&",$link[1]); for ($i=0;$i<count($ndata);$i++) {
$items = explode("=",$ndata[$i]); $data .= $items[0]."/".$items[1]."/";
}
} if ($data != "") {
$ret = $link[0]."/".$data;
} else {
$ret = $link[0];
} return $ret;
}
function contentRewrite($text) {
$txt = preg_replace_callback("/\<a href=\"([^\"]*)\"\>/",create_function('$match', 'return "<a href=\"".linkMorph($match[1])."\">";'), $text); return $txt;
}
/* Now you would add your switch to the top of the page in question to handle the content */
switch($page){
default:
$template = "main.html";
break;
case "links":
$template "links.html";
break;
// etc etc..
}
/* Now we just add in the index.php the following code where we want the content to be displayed
With the altered links */
$content = contentRewrite($template);
echo $content;
?>
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.