You need some sort of database for this, which maps an id
to its appropriate slug
. This could be as simple as a text file containing the mappings, e.g.
41960970 htaccess-url-rewriting-id-and-name
40587870 javascript-variable-in-bootstrap-tooltip
You can then define a RewriteMap
RewriteMap id2slug txt:/path/to/file.txt
and then use it in a rule
RewriteRule ^questions/([^/]+)$ /questions/$1/${id2slug:$1} [R,L]
This rule captures the id from the request and then redirects the client to the same URL with a slug appended.
Instead of a text file, you can also use other forms like dbm
, dbd
, or even an external program providing the mapping.
Be aware though, that RewriteMap
is only available in the main server or virtual host configuration.
When you don't have access to the main config, you might simulate this with a script doing the redirect, e.g.
RewriteRule ^questions/([^/]+)$ /id2slug.php?id=$1 [L]
and in the script, retrieve the slug from a database and send back
$id = $_GET['id'];
$slug = ...;
header('Location: http://www.example.com/questions/' . $id . '/' . $slug);