You can use .htaccess for this. For instance:
RewriteEngine On RewriteRule ^questions/(\d+)/([az-]+) index.php?id=$1&title=$2 [L]
Your PHP page (index.php) gets the identifier and title as parameters in $_GET[] :
echo $_GET['title'];
Then you can use this (or identifier, which is easier) to get the correct item from your data source:
// Assuming you didn't store the - in the database, replace them with spaces $real_title = str_replace("-", " ", $_GET['title']); $real_title = mysql_real_escape_string($real_title); // Query it with something like SELECT * FROM tbl WHERE LOWER(title) = '$real_title';
Assuming you have an id parameter in a URL of some type, it's easier to request it based on this value. Part of the header can only be used to make a readable URL, without having to act on it in PHP.
In reverse, to convert the header to format-like-this-to-use-in-urls , do:
$url_title = strtolower(str_replace(' ', '-', $original_title));
The above assumes that your headers do not contain characters that are illegal in the url ...
$article_link = "http://example.com/spowpost.php?post=$postid&$title=$url_title";
Or to submit to .htaccess:
$article_link = "http://example.com/spowpost$postid/$url_title";
source share