rewrites from nginx to htaccess
===

A friend is moving his website from nginx to apache (different host) but the rewrite rules are driving me nuts. I hope you guys can help!

**NGINX:**

        
    server {
        listen   80;
        server_name  foo.gg www.foo.gg;

    if ($host ~* www\.(.*)) {
                set $host_without_www $1;
                rewrite ^(.*)$ http://$host_without_www$1 permanent;
        }

	location / {
		if (!-e $request_filename) {
			rewrite  ^/clear$  /clear.php last;
			rewrite  ^/tools$  /tools.php last;
                        rewrite  ^/delete/([-a-zA-Z0-9]+)$  /delete.php?id=$1  last;
			rewrite  ^/([-a-zA-Z0-9]+)-$  /stats.php?code=$1  last;
			rewrite  ^/([-a-zA-Z0-9]+)$  /redirect.php?code=$1  last;
                        break;
		}
	}
    }

    server {
        listen   80;
        server_name  ~^(?<subdomain>.+)\.foo\.gg$;

	location / {
		if (!-e $request_filename) {
			rewrite  ^/([-a-zA-Z0-9]+)-$  /stats.php?code=$1&sub=$subdomain  last;
			rewrite  ^/([-a-zA-Z0-9]+)$  /redirect.php?code=$1&sub=$subdomain  last;
			break;
		}
	}
    }


Here's what I've managed so far with Apache:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.foo.gg$ [NC]
    RewriteRule ^(.*)$ http://foo.gg/$1 [L,R=301]

    RewriteRule ^tools$ tools.php [L]
    RewriteRule ^clear$ clear.php [L]
    RewriteRule ^tools$ tools.php [L]
    RewriteRule ^delete/([-a-zA-Z0-9]+)$ delete.php?id=$1 [L]

    RewriteRule ^([A-Za-z0-9-]+)-$ stats.php?code=$1
    RewriteRule ^([A-Za-z0-9-]+)$ redirect.php?code=$1


This satisfies the first part of the server rules, but not the funky bit with the subdomains.

Help!