URL重写重写x.php?id = 1

I've searched the site and got far enough where I've been successful at rewriting to a clean URL. Just need a bit more help.

I have a page with a record that I have successfully rewritten to a clean URL like so:

domain/record.php?id=1685 > to > domain/record-Gavin-Rees-1685 using the below:

.htaccess file:

RewriteRule ^record-(.*)-(.*)$ /record.php?id=$2 [L]

This in my php file:

$temp=str_replace(' ','-', $record [record_name]);
$temp=str_replace('.','', $temp);

<a href='/record-". $temp ."-".$record[id]." '>

This works perfect. The problem is.

I cannot get it to rewrite the other way so if you go directly to:

/record.php?id=1685 it still exists. i tried > RewriteRule ^record.php?id=$ /record-(.*)-(.*)$ [R,L]

This isn't possible in the .htacces rewrite rules, because you couldn't define a general rule, which knows about your software internals. If you want to do this in your .htaccess file, you have to create a rule for every single id which would result in a very large and unhandy .htaccess file.

The better way to get an redirection for direct script calls is, to do the redirect in the php file itself and set the http status codes(i.e. 301 - permanently moved). The get the requested url, you could use $_SERVER['REQUEST_URI'] and check for /record.php at the beginning