# Q: about "best" way for remove www in hostname in nginxish

**URL:** <https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747>\
**Category:** NGINX\
**Created:** [May 6, 2013, 6:40pm UTC](https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747 "2013-05-06T18:40:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [May 6, 2013, 6:40pm UTC](https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747/1 "2013-05-06T18:40:55Z")

</div>

Dear readers,

after reading

[http://nginx.org/en/docs/http/server\_names.html#regex\_names](http://nginx.org/en/docs/http/server_names.html#regex_names)

and googleing

[https://www.google.at/search?q=nginx+remove+www+subdomain](https://www.google.at/search?q=nginx+remove+www+subdomain)

I have a ‘best solution’ question.

I have the following customer request.

The ‘normal User’ type almost every time a www.subdomain.domain.at into  
they browser,  
which does not exist but the subdomain.domain.at exists.

I would now add the follwing into my nginx.conf.

### 

server {  
server\_name ~^(www.)?(?.+)$;

return http://$domain/;  
}

### 

Is this the cleanest way in nginxish?

Thanks for help  
Aleks

---

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [May 6, 2013, 6:49pm UTC](https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747/2 "2013-05-06T18:49:41Z")

</div>

I just do

server {  
listen 80;  
server\_name [was.foo.com](http://was.foo.com);  
rewrite ^ [http://foo.com$uri](http://foo.com$uri) permanent;  
}

---

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [May 6, 2013, 6:50pm UTC](https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747/3 "2013-05-06T18:50:16Z")

</div>

That’s how I do it, except that I hard-code a list of the domains I  
want to redirect. Otherwise, I’d have built a service that could be  
trivially used by anyone else for their own domain. Wildcards are bad,  
mmkay? 😉

IMHO and YMMV,  
Jonathan

---

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [May 6, 2013, 8:31pm UTC](https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747/4 "2013-05-06T20:31:58Z")

</div>

## I do: if ($host ~\* "^www.(._)$") { set “$domain” “$1”; rewrite (._) http://$domain$uri permanent; }

_B. R._

On Mon, May 6, 2013 at 12:50 PM, Jonathan M.

---

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [May 6, 2013, 10:20pm UTC](https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747/5 "2013-05-06T22:20:38Z")

</div>

I remember this and now also where I have seen it 😉

> **[Pitfalls and Common Mistakes | NGINX](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/)**
>
> This page outlines some of the NGINX configuration issues that we see frequently and then explains how to resolve them.

Thanks for all feedback.

I will try the Solution written in first post.

Br aleks

Am 06-05-2013 20:30, schrieb B.R.:

> > removed\_email\_address@domain.invalid  
> > [nginx Info Page](http://mailman.nginx.org/mailman/listinfo/nginx) [1]
> 
> * * *
> 
> nginx mailing list  
> removed\_email\_address@domain.invalid  
> [nginx Info Page](http://mailman.nginx.org/mailman/listinfo/nginx) [1]

Links:

---

<div class="post-metadata">

**Author:** ![Aleksandar\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@Aleksandar\_L](https://www.ruby-forum.com/u/Aleksandar_L)\
**Post date:** [May 7, 2013, 4:26am UTC](https://www.ruby-forum.com/t/q-about-best-way-for-remove-www-in-hostname-in-nginxish/228747/6 "2013-05-07T04:26:19Z")

</div>

You’re right, my solution sucks… but it has the benefit of being  
generic.  
I’ll look into it.

## Glad you found a solution, though. ;o)

_B. R._
