Making a dynamic URL look like static

 

 

1. Introduction
Static URLs are preferred by search engines and are more user-friendly and meaningful than the dynamic ones.
An example of dynamic URL is:
http://example.com/page.php?id=1545

of course this URL tells nothing to the user and search engines won’t like it, because they will know it is dynamically generated.

2. Example codes
So let’s say that page.php looks something like this:

<?php
/*assuming we have made a function get_page_content which gets the content of the page from the id we have. This could come form mysql or anywhere else */
$content = $this->get_page_content(addslashes($_GET['id']));

//then we just display the content:
echo $content;
....
?>

And let’s say the get_page_content() function, looks something like this (just example, don’t use it):
function get_page_content($id) {
//initialise mysql conneciton (using another function we have made called sqlconnect())
$this->sqlconnect();
//avoid any surprises (you've also already called addslashes() to this id):
$id = mysql_real_escape_string($id);
//set up query:
$query = "SELECT content FROM `pages` WHERE `id`='$id'";
//execute query
$result = mysql_query($query);
//create an array from the result
$row = mysql_fetch_array($result);
//get the first index in the array:
$content = $row[0];
//set it as the function result
return $content;
}

An example static URL is:

http://example.com/1545.html

Of course, this will ask the web server for the static file 1545.html which doesn’t exist.

If we want to display the same dynamic page from above page when we request it
(and we have an apache web server with mod_rewrite enabled) then we just create a new .htaccess file with the following two lines:
RewriteEngine on
RewriteRule ^([^/]*).html$ page.php?id=$1 [L]

In that way, we tell the web server to read the content from the dynamic url address and display it under the static without letting the user know.

However, 1545.html is not looking exactly like a static URL, due to the meaningless number it actually is.

For that reason, we have to re-design our web system, and add a new identifier for the content item, which is more meaningful.

3. Creating a new identifier
That is a very easy job to do as well. First you need to alter your database table and insert a new column (for example called “page”), then you need to alter your get_page_content() function and change the SQL query so that it gets the content from the new identifier, not from the old “id” column.

So our example function will look like this:
function get_page_content($page) {
//initialise mysql conneciton (using another function we have made called sqlconnect())
$this->sqlconnect();
//avoid any surprises (you've also already called addslashes() to this identifier):
$id = mysql_real_escape_string($page);
//set up query:
$query = "SELECT content FROM `pages` WHERE `page`='$page'";
//execute query
$result = mysql_query($query);
//create an array from the result
$row = mysql_fetch_array($result);
//get the first index in the array:
$content = $row[0];
//set it as the function result
return $content;
}

Of course we also need to change our page.php file, where we are calling the function:
<?php
$content = $this->get_page_content(addslashes($_GET['page']));
echo $content;
?>

So now, we need to populate the pages table with the new identifier which will be the virtual filename of the new static address. Please note that you should use only symbols which are valid for both URLs and UNIX system file names. And you should avoid special characters.

After you have populated it, try calling the new dynamic address of the page like this:
http://example.com/page.php?page=new-identifier-value

If this displays correctly, go ahead and alter the .htaccess file to read:

RewriteEngine on
RewriteRule ^([^/]*).html$ page.php?page=$1 [L]

Now, try calling the new URL:

http://example.com/new-identifier-value.html

That’s all! Simple, a? :))

P.S. If mod_rewrite or rewriting is disabled on the web server, you will need ot enable it. Simply edit httpd.conf and uncomment before mod_rewrite loader line (remove the hash (#) sign in the start).
After that find the <directory> bit and see if “AllowOverride” is set to “All”, if it’s set to “None”, change it to “All”.