RewriteCond %{THE_REQUEST} " /portfolio\.php\?id=(\d+)"
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([0-9]+)$ /portfolio.php?id=$1 [L,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond &%{QUERY_STRING} &id=(\d+)
RewriteRule ^portfolio\.php$ /%1? [R=301,L]
RewriteRule ^([0-9]+)$ /portfolio.php?id=$1 [L,QSA]
RewriteCond %{QUERY_STRING} (^|&)id\=([0-9]{1,})($|&)
RewriteRule ^portfolio\.php$ /%2? [L,R=301]