.htaccess
RewriteEngine On RewriteRule ^game/([0-9]+)/ /game.php?newid=$1
game.php
if (isset($_GET['id'])) { $row = dbgetrow("SELECT * FROM games WHERE id = %s",$_GET['id']); if ($row) { Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: /game/".$row['id']."/".seo_title($row['name'])); } else { Header( "HTTP/1.1 404 Not found" ); } exit; } if (isset($_GET['newid'])) $_GET['id'] = $_GET['newid'];
this is code that works for me but is considered pseudo code to give you an idea.
Dbgetrow () function to get a row from a database
and the seo_title () function might look like this
function seo_title($s) { $s = strtolower(trim($s)); $s = str_replace(" ","-",$s); $s = preg_replace('![^a-z0-9-]!',"",$s); $s = preg_replace('!\-+!',"-",$s); return $s; }
The whole concept was sandwiched from SO :)
the only part of games/1/
really important, and the rest can be anything, the name of the game is for seo purposes only. Take this question title, for example:
How can I use in .htaccess? :)
source share