Within .htaccess or apache2.conf
Rewrite Engine On
Rewrite Rule ^(www\.)?([a-zA-Z]+)\.(.{1-5})$ cgi-bin/test.cgi/letters/http/$1$2$3
Rewrite Rule ^addname.php$ cgi-bin/addname.cgi
Rewrite Rule ^add/?$ cgi-bin/addname.cgi
EDIT:
Rewrite Engine On
Rewrite Rule ^add/(.*)$ cgi-bin/addname.cgi$1
Rewrite Rule ^addname.php(\?(.*))$ cgi-bin/addname.cgi$1
RewriteCond %{REQUEST_URI} !^/$
Rewrite Rule ^((.*)\.)?(.*)(\.[A-Za-z\.]{2,8})(/.*)$ cgi-bin/test.cgi/?subdomain=$1&urlname=$2&tld=$3&path=$4 [L]
Fixed and actually tested the regex this time.
The $1, $2,$3,$4 match the parts of the domain.
The whole domain can be specified simply by using $1$2$3$4
I used a domain regex so only domains will be matched, and other website paths would not.