Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;
    break;
}
}
}

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]

Help?

share|improve this question

1 Answer 1

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

 
discard

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.