nginx rewrite all wildcard subdomains to www.site.com [closed] nginx rewrite all wildcard subdomains to www.site.com [closed] nginx nginx

nginx rewrite all wildcard subdomains to www.site.com [closed]


The best way to do this in nginx is with a combination of two server blocks:

server {  server_name *.example.org;  return 301 $scheme://example.org$request_uri;}server {  server_name www.example.org;  #add in further directives to serve your content}

I have tested this on my laptop, since you reported it not working. I get the following result locally (after adding www2.test.localhost and www.test.localhost to my /etc/hosts, along with the nginx config bit, and reloading nginx):

$ curl --head www2.test.localhostHTTP/1.1 301 Moved PermanentlyServer: nginx/1.2.6Date: Thu, 07 Mar 2013 12:29:32 GMTContent-Type: text/htmlContent-Length: 184Connection: keep-aliveLocation: http://www.test.localhost/

So yes, this definitely works.


server {    server_name .example.com;    return 301 http://www.example.com$request_uri;}server {    server_name www.example.com;    [...]}

References: