Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A friend of mine is moving hosts and I've been tasked with sorting out his rewrite rules. I've got most of them done, barring this awkward subdomain rewrite rule:

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;

I've tried this (from another serverfault thread, which doesn't work for me:

RewriteCond %{HTTP_HOST} ^([^.]+)\.foo\.gg$

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


share|improve this question

The replace string needs to be adjusted to use the capture from the RewriteCond, and whether or not you need the leading slash depends on where this RewriteRule is configured - the way you have it means that it needs to be in the <Directory> block for the document root, or an .htaccess file there.

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

If it's in your <VirtualHost> then keep the leading slash.

RewriteRule ^/([A-Za-z0-9-]+)$ redirect.php?code=$1&sub=%1 [L]
share|improve this answer
+1 for explaining when you need a trailing slash. I'm always having troubles with that with Apache. – khoxsey Jul 27 '12 at 3:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.