BeWebmaster

Search engine friendly URLs for dynamic content

Ad

This is a simple php solution for creating search engine friendly URLs. Google is one of few search engines that indexes URLs containing question-mark "?" and similar symbols that are unique to dynamically served websites. Most search engines do not want to deal with such complicated URLs and as a result the dynamic content or pages that aren't easily discovered by following links may not get indexed.

There are couple of ways to fix this problem and in this article I will show you how you can modify your current scripts that execute your PHP dynamic content by just adding couple of lines of code. The added code will actually create a php file that will have the news release id variable and include the script that displays the news release.

It may sound a little confusing but it is actually very easy to do.

This tutorial assumes that you already have a dynamic web site and will not go through steps of creating one.

Common Setup and Assumptions

Let's assume we have a website with news articles stored in the database. The second assumption is that you are using auto_increment to set up your article id in the news release table.  Let's also assume you are using script called news.php to display the content, in which case the link that calls for the script would look similar to:"http://yourdomain.com/news.php?id=12"

How to do it?

To save the article to the database we would have a script that executes a MYSQL query. See example below.

$qry = "INSERT INTO news ( title , content ) VALUES ( ' $title ' , ' $content ') " ;
$run = mysql_query( $qry ) or die ( " Error in query: $qry. " . mysql_error() ) ;

The first step in creating our search engine friendly url is to get the id number of just saved article, or any other info that we would normally pass to the script that displays the content. One way to get the id number is to execute another query and get last added id. See example.

$qry = " SELECT * FROM news WHERE title = '$title' " ;
$run = mysql_query( $qry ) or die ( " Error in query: $qry. " . mysql_error() ) ;
$row = mysql_fetch_object( $run );
$id = $row->id;

Now that we have id number we can create the file.

$filename = $id.".php" ;
$filedata = "<?php
$id=$id;
include("news.php");
?>
  "
;    
  if (!$file_handle = fopen($filename, "a" )) {
   $error = "Cannot open file" ;
  }  
  if (!fwrite($file_handle, $filedata)) {
   $error =  "Cannot write to file" ;
  }     
  fclose($file_handle);

The script above creates actual file using a news release id number as a file name. In other words, instead of sending "id" to the news.php file using "http://yourdomain.com/news.php?id=12" we just create a file that already has the "id" and includes news.php underneth it. The file contains only couple of lines of code making it small enough to not significantly affect your web space. Still this method is not recommended for huge dynamic websites.

If you are using some type of the script that deletes the article you need to add following line in the script that deletes the article from the database:

unlink($id.".php");

You can place this line of code right after executing the MYSQL query that deletes the record.