And so do rewritten my ugly php Hyperlink to something more attractive:

RewriteEngine On
RewriteRule ^main/photo([^/\.]+)/?.html$ main/details.php?gid=$2pid=$1

However, now I wish to pressure anybody who would go to

http://www.example.com/main/details.php?gid=20&pid=5

to redirect to

htto://www.example.com/main/photo5.html

I've attempted the next RewriteCond:

RewriteCond %{REQUEST_URI} ^/main/details\.php [NC]
RewriteRule ^main/details.php?gid=(.*)&pid=(.*) http://www.example.com/main/photo$1.html [R=301,L]

But that did not work. Any ideas?

You have to consider the request line to find out when the current request URI may be the original. Furthermore you should utilize the RewriteCond directive to check the query from the URI:

RewriteCond %{THE_REQUEST} ^GET\ /main/details\.php
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pid=([0-9]+)(&.*)?$
RewriteRule ^main/details\.php$ /main/photo%3.html? [L,R=301]