default nginx rewrite with regexp

Yoann Moulin yoann at
Mon Nov 17 13:22:48 UTC 2014


I'm working on a default server_name that match everything but www.domain.tld
then rewrite to www.domain.tld. I use nginx 1.2.1-2.2+wheezy3 with a big regexp.

I have an issu with tld that contain a dot (, etc...).

I must be able to make a difference between and
first must rewrite second, to

Previously, I had this regexp that does not working with any tld that contain a
dot :

> server_name ~^((?<subdomain>(w(w[^w]?.*)?)|([^w]?.*))\.)?(?<domain_name>[^.]+)\.(?<tld>[^.]+)$;
> rewrite ^ http://www.$domain_name.$tld$request_uri? permanent ;

Now I tryto do something like that :

> server_name "~^((?<subdomain>(w(w[^w]?.*)?)|([^w]?.*))\.)?(?<domain_name>[^.]+)(?<tld>((\.[^.]{1-3}\b){1,2}))$";
> rewrite ^ http://www.$domain_name$tld$request_uri? permanent ;

But I don't find any clean solution. Is there a solution for that ? or I must
still list all domains in my vhost ?

Best regards

Yoann Moulin

More information about the nginx mailing list