# Redirect www to no-www with variable (for multiple domains)?

**URL:** <https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718>\
**Category:** NGINX\
**Created:** [July 7, 2013, 11:31am UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718 "2013-07-07T11:31:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [July 7, 2013, 11:31am UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718/1 "2013-07-07T11:31:21Z")

</div>

According to [Pitfalls and Common Mistakes | NGINX](http://wiki.nginx.org/Pitfalls#Server_Name) this is the best  
solution to redirect www to no-www for one domain:

server {  
server\_name [www.domain.com](http://www.domain.com);  
return 301 $scheme://domain.com$request\_uri;  
}  
server {  
server\_name [domain.com](http://domain.com);  
[…]  
}

Is there a way to do this with regex or variables? I’ve ~15 domains and  
it  
would be more convenient to have only one entry “to rule them all” 😉

Posted at Nginx Forum:

> **[Redirect www to no-www with variable (for multiple domains)?](https://forum.nginx.org/read.php?2,240622,240622#msg-240622)**
>
> According to http://wiki.nginx.org/Pitfalls#Server\_Name this is the best solution to redirect www to no-www for one domain: server { server\_name www.domain.com; return 301 $scheme://domain.com$request\_uri; } server { server\_name domain.com;

---

<div class="post-metadata">

**Author:** ![lennart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@lennart](https://www.ruby-forum.com/u/lennart)\
**Post date:** [July 7, 2013, 1:43pm UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718/2 "2013-07-07T13:43:56Z")

</div>

On 7 Jul 2013 10:30, “lennart” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> […]  
> }
> 
> Is there a way to do this with regex or variables? I’ve ~15 domains and it  
> would be more convenient to have only one entry “to rule them all” 😉

Absolutely there are ways to do this all in one go. Have a try and let  
us  
know where you get stuck 🙂

Jonathan

---

<div class="post-metadata">

**Author:** ![lennart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@lennart](https://www.ruby-forum.com/u/lennart)\
**Post date:** [July 7, 2013, 2:23pm UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718/3 "2013-07-07T14:23:04Z")

</div>

## Jonathan M. Wrote:

> > server {  
> > let us  
> > know where you get stuck 🙂
> 
> Jonathan
> 
> * * *

As a newbie to NGINX (used 10yrs APACHE before) i don’t know the exact  
route  
on the wiki & forum ;-). I also not found a newbie-section 😉 But off  
course, i shall post the solution.

Sajan tells about $host, i’ll give it a try

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,240622,240625#msg-240625](http://forum.nginx.org/read.php?2,240622,240625#msg-240625)

---

<div class="post-metadata">

**Author:** ![lennart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@lennart](https://www.ruby-forum.com/u/lennart)\
**Post date:** [July 7, 2013, 8:48pm UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718/4 "2013-07-07T20:48:03Z")

</div>

I should correct my server\_name line. It should not have the variable,  
but the actual domain names. So I guess you’ll still have to come back  
and add each domain.

Sorry. On phone.

Sajan P.

---

<div class="post-metadata">

**Author:** ![lennart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@lennart](https://www.ruby-forum.com/u/lennart)\
**Post date:** [July 7, 2013, 1:48pm UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718/5 "2013-07-07T13:48:37Z")

</div>

Haven’t testing this, but would you not be able to replace ‘[domain.com](http://domain.com)’  
with $host?

server {  
server\_name www.$host;  
rewrite ^(.\*) $scheme://$host$request\_uri permanent;  
}

That should work I think. Right now, I do it the way you’ve descrived  
individually in all my .conf files for each domain.

- Sajan P.

---

<div class="post-metadata">

**Author:** ![lennart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@lennart](https://www.ruby-forum.com/u/lennart)\
**Post date:** [July 8, 2013, 6:48am UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718/6 "2013-07-08T06:48:31Z")

</div>

On Sun, Jul 7, 2013 at 6:30 PM, lennart [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to do this with regex or variables? I’ve ~15 domains and it  
> would be more convenient to have only one entry “to rule them all” 😉

the regex way, which is supposedly slower:

server {  
server\_name ~^www.(?.+)$;  
listen 80; listen [::]:80;  
return 301 $scheme://$domain$request\_uri;  
}

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![lennart](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@lennart](https://www.ruby-forum.com/u/lennart)\
**Post date:** [July 8, 2013, 11:48pm UTC](https://www.ruby-forum.com/t/redirect-www-to-no-www-with-variable-for-multiple-domains/245718/7 "2013-07-08T23:48:00Z")

</div>

## Edho A. Wrote:

> server {  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

‘supposedly slower’ is a very good argument to not use RegEx 😉 Do you  
have  
a source for that?

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,240622,240679#msg-240679](http://forum.nginx.org/read.php?2,240622,240679#msg-240679)
