# Rewrite regexp for subdomains

**URL:** <https://www.ruby-forum.com/t/rewrite-regexp-for-subdomains/198372>\
**Category:** NGINX\
**Created:** [October 30, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/rewrite-regexp-for-subdomains/198372 "2010-10-30T20:09:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 30, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/rewrite-regexp-for-subdomains/198372/1 "2010-10-30T20:09:22Z")

</div>

Hi i need to [blabla.example.org](http://blabla.example.org) or [dummy.example.org](http://dummy.example.org) anotherdummy send  
me to [www.example.org](http://www.example.org) (another words \*.example.org must redirect to  
[www.example.org](http://www.example.org)) but when i type [www.example.org](http://www.example.org) it can’t send to  
[www.example.org](http://www.example.org) cause it’s loop. Help me to realize this construction.

Posted at Nginx Forum:

> **[Rewrite regexp for subdomains](https://forum.nginx.org/read.php?2,146043,146043#msg-146043)**
>
> Hi i need to blabla.example.org or dummy.example.org anotherdummy send me to www.example.org (another words \*.example.org must redirect to www.example.org) but when i type www.example.org it can't send to www.example.org cause it's loop. Help me to...

---

<div class="post-metadata">

**Author:** ![shitit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@shitit](https://www.ruby-forum.com/u/shitit)\
**Post date:** [October 31, 2010, 2:17am UTC](https://www.ruby-forum.com/t/rewrite-regexp-for-subdomains/198372/2 "2010-10-31T02:17:25Z")

</div>

On 30 Out 2010 19h08 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi i need to [blabla.example.org](http://blabla.example.org) or [dummy.example.org](http://dummy.example.org) anotherdummy  
> send me to [www.example.org](http://www.example.org) (another words \*.example.org must  
> redirect to [www.example.org](http://www.example.org)) but when i type [www.example.org](http://www.example.org) it  
> can’t send to [www.example.org](http://www.example.org) cause it’s loop. Help me to realize  
> this construction.

Create two server blocks. Put the www server name before the \* and it  
should work:

See [Server names](http://nginx.org/en/docs/http/server_names.html)

Exact names precede patterns or regex server names.

— appa

---

<div class="post-metadata">

**Author:** ![shitit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@shitit](https://www.ruby-forum.com/u/shitit)\
**Post date:** [October 31, 2010, 8:11pm UTC](https://www.ruby-forum.com/t/rewrite-regexp-for-subdomains/198372/3 "2010-10-31T20:11:17Z")

</div>

On 31 Out 2010 03h48 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It doesn’t work. With server\_name [www.example.org](http://www.example.org) and server\_name  
> \*.example.org my site doesn’t redirect [blabla.example.org](http://blabla.example.org) to  
> [www.example.org](http://www.example.org)

server {  
listen [::]:80; # ipv6

```
   server_name www.example.com;
   (...)

```

}

server {  
## This is to avoid the spurious if for sub-domain name  
## rewriting. See [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/Pitfalls#Server_Name).  
listen [::]:80; # ipv6  
server\_name \*.example.com;  
rewrite ^ $scheme://www.example.com$request\_uri permanent;  
} # server domain rewrite.

I haven’t tried it myself. Try it.

— appa

---

<div class="post-metadata">

**Author:** ![shitit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@shitit](https://www.ruby-forum.com/u/shitit)\
**Post date:** [October 31, 2010, 4:50am UTC](https://www.ruby-forum.com/t/rewrite-regexp-for-subdomains/198372/4 "2010-10-31T04:50:15Z")

</div>

It doesn’t work. With server\_name [www.example.org](http://www.example.org) and server\_name  
\*.example.org my site doesn’t redirect [blabla.example.org](http://blabla.example.org) to  
[www.example.org](http://www.example.org)

Posted at Nginx Forum:

> **[Re: Rewrite regexp for subdomains](https://forum.nginx.org/read.php?2,146043,146134#msg-146134)**
>
> Hi i need to blabla.example.org or dummy.example.org anotherdummy send me to www.example.org (another words \*.example.org must redirect to www.example.org) but when i type www.example.org it can't send to www.example.org cause it's loop. Help me to...
