wildcard to rewritre multi domains to www

Cliff Wells cliff at develix.com
Mon Sep 7 22:06:41 MSD 2009

On Mon, 2009-09-07 at 13:44 +0300, Marcus Clyne wrote:
> Hi,
> >> I think if you look at your regex again, you'll see that it's not that 
> >> it doesn't match any domains with sub domains, it matches any domains 
> >> with a dot somewhere in the middle.  The \. guarantees that there's a 
> >> dot, but the other characters can be anything.
> >>     
> >
> > Ah, so instead of .+ it'd need to be [^.]+ as in:
> >
> > server_name ~(^[^.]+\.[^.]+$);
> >   
> That would be fine - for domains with just the top level domain before 
> the main name, e.g. test.com.  However, if you also deal with URLs with 
> a country code as well, e.g. test.co.uk, it's not going to capture them 
> because they'll look like the subdomains you were trying to avoid 
> capturing.  You could create a rather complex RE using all the country 
> codes and tld's to fully capture only the main domain, and no 
> sub-domains, but it'd be pretty long and would probably waste 
> considerable processing time.  Personally, I like to keep things simple. :-)

Well, my guess is that there's no general regex that's going to cover
the case of say:

files.domain.com -> files.domain.com
test.co.uk -> www.test.co.uk

I think the OP would have to make a choice based on his subset of
domains or setup special locations for places like files.domain.com
(which would probably be the right solution).


More information about the nginx mailing list