# Rewrite rule for all domains

**URL:** <https://www.ruby-forum.com/t/rewrite-rule-for-all-domains/228280>\
**Category:** NGINX\
**Created:** [April 16, 2013, 4:48pm UTC](https://www.ruby-forum.com/t/rewrite-rule-for-all-domains/228280 "2013-04-16T16:48:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_S\_StyleFlare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@David\_S\_StyleFlare](https://www.ruby-forum.com/u/David_S_StyleFlare)\
**Post date:** [April 16, 2013, 4:48pm UTC](https://www.ruby-forum.com/t/rewrite-rule-for-all-domains/228280/1 "2013-04-16T16:48:10Z")

</div>

Pardon me if I missed this in the docs…

My issue is that I want to rewrite every domain and not create a server  
block for each.

I am trying to rewrite every domain thats pointing to Nginx

from [www.server.com](http://www.server.com) to [server.com](http://server.com)

currently I am doing uwsgi\_pass unix://$host.sock

but if the host name has the www prefix then my socket is not found…

Currently I am using the default nginx.conf file with only one line  
added uwsgi\_pass

Thanks in advance for any help.

---

<div class="post-metadata">

**Author:** ![David\_S\_StyleFlare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@David\_S\_StyleFlare](https://www.ruby-forum.com/u/David_S_StyleFlare)\
**Post date:** [April 16, 2013, 5:08pm UTC](https://www.ruby-forum.com/t/rewrite-rule-for-all-domains/228280/2 "2013-04-16T17:08:04Z")

</div>

On 16 April 2013 15:47, David | StyleFlare [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Pardon me if I missed this in the docs…
> 
> My issue is that I want to rewrite every domain and not create a server  
> block for each.
> 
> I am trying to rewrite every domain thats pointing to Nginx
> 
> from [www.server.com](http://www.server.com) to [server.com](http://server.com)

Have a single separate server block do it for you:

server {  
listen 80;  
server\_name ~^(www.)(?.+)$;  
rewrite $scheme://$domain$uri$is\_args$args;  
}

(written but not tested; YMMV!)

Jonathan

---

<div class="post-metadata">

**Author:** ![David\_S\_StyleFlare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@David\_S\_StyleFlare](https://www.ruby-forum.com/u/David_S_StyleFlare)\
**Post date:** [April 16, 2013, 5:15pm UTC](https://www.ruby-forum.com/t/rewrite-rule-for-all-domains/228280/3 "2013-04-16T17:15:40Z")

</div>

Thanks, I will test it.

Appreciate it.

---

<div class="post-metadata">

**Author:** ![David\_S\_StyleFlare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@David\_S\_StyleFlare](https://www.ruby-forum.com/u/David_S_StyleFlare)\
**Post date:** [April 16, 2013, 5:19pm UTC](https://www.ruby-forum.com/t/rewrite-rule-for-all-domains/228280/4 "2013-04-16T17:19:46Z")

</div>

Adding to Jonathan suggestion with a twist:

1. Use a map directive at the http level:

2. Create a default server while leaving all vhosts using only the base  
domain.

----appa
