General warning:Before doing any on-site work, do not forget to back up your original files. Now that we know the basic files that need to be edited to give your site the most relevant and unique page titles, we will come up with a good strategy to change those titles into something we want.
PHP script for producing unique titles for the home page and category pages (actual script in blue): <head> <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>"> <?php $optimizedurl=$_SERVER["REQUEST_URI"]; if (preg_match("/cPath/i", $optimizedurl)) { //this is a category page, extract the category name $location1=(strpos($optimizedurl,'cPath')); $start= $location1 + 6; $categoryextracted=substr($optimizedurl,$start,2); $result = mysql_query("SELECT `categories_name` FROM `categories_description` WHERE `categories_id`='$categoryextracted'") or die(mysql_error()); $row = mysql_fetch_array($result) or die("Invalid query: " . mysql_error()); echo "<br />"; $titletag = $row['categories_name']; } else //this is the root page { $titletag = 'Put your SEO optimized title tag here for the homepage'; } ?> <title><?php echo $titletag ?></title> Let me explain how the PHP script above will get the appropriate page title. It starts with $optimizedurl=$_SERVER["REQUEST_URI"]; This script will get the URL of the page and store it to a variable. Note that$_SERVER["REQUEST_URI"]is not perfectly compatible with Windows Server, and there are things that need to be adjusted; explaining them is beyond the scope of this tutorial. However, you can read it here: http://www.php.net/reserved.variables Now, since the index.php will cover both the home page and the category pages, we need to determine whether the URL analyzed by PHP is for a category page or the root URL. This script will provide the answer: if (preg_match("/cPath/i", $optimizedurl)) { Category pages containcPath in the URL, and by using the preg_matchfunction, we can test to see if it contains this variable. If it is a category page, the next job of the script is getting the category ID, which will be used to pull out the category name in the MySQL database: //this is a category page, extract the category name $location1=(strpos($optimizedurl,'cPath')); $start= $location1 + 6; $categoryextracted=substr($optimizedurl,$start,2); $result = mysql_query("SELECT `categories_name` FROM `categories_description` WHERE `categories_id`='$categoryextracted'") or die(mysql_error()); $row = mysql_fetch_array($result) or die("Invalid query: " . mysql_error()); echo "<br />"; $titletag = $row['categories_name']; The combination ofstrposandsubstr will extract the category ID from the URL. For example: http://localhost/osc/index.php?cPath=4&osCsid=2a003844f541432aa794b92c2ece050d The category ID in the above URL is the value of the cPath variable, which is 4. The rest of the above script's purpose will be to query the database in order to extract the category name.
blog comments powered by Disqus |
|
|
|
|
|
|
|