# Redirect (almost) all subdomains to www?

**URL:** <https://www.ruby-forum.com/t/redirect-almost-all-subdomains-to-www/195703>\
**Category:** NGINX\
**Created:** [September 28, 2010, 3:15pm UTC](https://www.ruby-forum.com/t/redirect-almost-all-subdomains-to-www/195703 "2010-09-28T15:15:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [September 28, 2010, 3:15pm UTC](https://www.ruby-forum.com/t/redirect-almost-all-subdomains-to-www/195703/1 "2010-09-28T15:15:02Z")

</div>

I have two “server” directives.

One has:

server\_name [www.example.com](http://www.example.com);

The other has:

server\_name [admin.example.com](http://admin.example.com);

Both do what they should. However, right now any subdomain (besides  
“[admin.example.com](http://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](http://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](http://example.com))

Basically all requests should be directed to have the www subdomain  
(except for [admin.example.com](http://admin.example.com))

What’s the best way to do this?

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [September 28, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/redirect-almost-all-subdomains-to-www/195703/2 "2010-09-28T15:32:41Z")

</div>

> What’s the best way to do this?

> **[Converting rewrite rules](http://nginx.org/en/docs/http/converting_rewrite_rules.html)**

> **[Server names](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](http://www.yourdomain.com) .

rr

---

<div class="post-metadata">

**Author:** ![thomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e36b37/32.png) [@thomas](https://www.ruby-forum.com/u/thomas)\
**Post date:** [September 28, 2010, 3:49pm UTC](https://www.ruby-forum.com/t/redirect-almost-all-subdomains-to-www/195703/3 "2010-09-28T15:49:39Z")

</div>

On 28 Set 2010 14h15 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Both do what they should. However, right now any subdomain (besides  
> “[admin.example.com](http://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](http://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](http://example.com))
> 
> Basically all requests should be directed to have the www subdomain  
> (except for [admin.example.com](http://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](http://www.example.com);  
}

You can remove the default\_server flag if you can guarantee that  
[www.example.com](http://www.example.com) is the _first_ one that appears inside your http  
block.

— appa
