0

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?

1 Answer 1

2

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]
1
  • +1 for explaining when you need a trailing slash. I'm always having troubles with that with Apache.
    – khoxsey
    Commented Jul 27, 2012 at 3:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .