I've got a problem which i cannot wrap my mind around.

I am using Apache and PHP

I have to get:

http://localhost.com/cat_ap.php?nid=5964

from

http://localhost.com/cat_ap~nid~5964.htm

How do you start altering that around? I've done simpler mod rewrites but this really is a little more complicated. Can anybody produce an advantage or point me within the right direction

RewriteRule ^/cat_ap~nid~(.*)\.htm$ /cat_ap?nid=$1 [R]

The [R] in the finish is optional. Should you omit it, Apache will not redirect your customers (it'll still serve the right page).

When the nid part is another variable, you can test this:

RewriteRule ^/cat_ap~([^~]+)~(.*)\.htm$ /cat_ap?$1=$2 [R]

EDIT: As Ben Blank stated in the comment, you might like to restrict the group of valid Web addresses. For instance, you might like to make certain a nid is available, which it's statistical:

RewriteRule ^/cat_ap~nid~([0-9]+)\.htm$ /cat_ap?nid=$1

or maybe the nid part is really a variable, it only includes alphabetical figures:

RewriteRule ^/cat_ap~([A-Za-z]+)~([0-9]+)\.htm$ /cat_ap?$1=$2

Presuming the variable parts listed here are the "nid" and also the 5964, that you can do:

RewriteRule ^/cat_ap~(.+)~(.+).htm$ ^/cat_ap?$1=$2

The very first "(.+)" matches "nid" and also the second matches "5964".

If you would like everything arbitrary:

RewriteRule ^/(\w+)~(\w+)~(\w+)\.htm$ $1?$2=$3 [L]

Where \w is equivalent to [A-Za-z0-9_]. And when you need to make use of this rule inside a .htaccess file, take away the leading / in the pattern.