Redirect (almost) all subdomains to www?

I have two “server” directives.

One has:

server_name www.example.com;

The other has:

server_name admin.example.com;

Both do what they should. However, right now any subdomain (besides
admin.example.com”) is the exact same as www. I would prefer if things
were redirected so that a user cannot use “randomstuff.example.com” to
see the website. I also don’t want them to be able to use it without a
www (e.g. example.com)

Basically all requests should be directed to have the www subdomain
(except for admin.example.com)

What’s the best way to do this?

What’s the best way to do this?

http://nginx.org/en/docs/http/converting_rewrite_rules.html
http://nginx.org/en/docs/http/server_names.html

Should explain how to do that - in short you make a default “catch all”
server block and redirect to www.yourdomain.com .

rr

On 28 Set 2010 14h15 WEST, [email protected] wrote:

Both do what they should. However, right now any subdomain (besides
admin.example.com”) is the exact same as www. I would prefer if
things were redirected so that a user cannot use
randomstuff.example.com” to see the website. I also don’t want them
to be able to use it without a www (e.g. example.com)

Basically all requests should be directed to have the www subdomain
(except for admin.example.com)

If I understand correctly your question, something like this should to
the trick:

server {
listen [::]:80 default_server;
server_name www.example.com;
}

You can remove the default_server flag if you can guarantee that
www.example.com is the first one that appears inside your http
block.

— appa