.htaccess ReWrite to stop external referrer

Ran into an issue recently where a REALLY old perl script was being used to redirect to an external site that.. well wasn’t work safe.  This script was last up dated in 1999.  Yes.. 1999. as in the year.  Basically the spammer was using this script

http://example.com/cgi/script.cgi?http://link.tonsfw.page/lots-of-icky-stuff/

Sadly my perl skills are horrible and after four hours of fighting with it I thought .htaccess might be a better option.  After about 20 minutes I had the start of the file and in 30 minutes I had it fixed.  here is what my .htaccess for that directory now looks like.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^http://www\.example\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://example\.com/ [NC]
RewriteRule ^ . [R=404,L]

Any one who clicks on a link to that script that does not originate from the domain will now get a 404 error.