# Remove www from the url

**URL:** <https://www.ruby-forum.com/t/remove-www-from-the-url/203305>\
**Category:** NGINX\
**Created:** [February 17, 2011, 3:46pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305 "2011-02-17T15:46:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 17, 2011, 3:46pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/1 "2011-02-17T15:46:42Z")

</div>

Hello everyone,

I used to use the following trick to cut the initial www. from the  
address so that [www.mysite.com](http://www.mysite.com) would redirect to [mysite.com](http://mysite.com):

```auto
server {
    server_name mysite.com www.mysite.com;

    if ($host ~* www\..*) {
        rewrite ^(.*)$ $1 permanent;
    }
```

Well, this method no longer works. I do not know when it stopped working  
but I will get “The page isn’t redirecting properly” in Firefox. It  
still puzzles me the documentation still implies this should work:

[quote]“Note that if a redirect is relative (has no host part), then  
when redirecting Nginx uses the “Host” header if the header match name  
of server\_name directive or the first name of server\_name directive, if  
the header does not match or is absent. If no server\_name is set, then  
the local hostname is used. If you want Nginx to always use the “Host”  
header, you can use a wildcard “\*” server\_name (but see the restrictions  
on doing so)”[/quote]

Why is that and more importantly, what trick should I use to redirect  
[www.mysite.com](http://www.mysite.com) → [mysite.com](http://mysite.com)?

Thank you!

Posted at Nginx Forum:

> **[remove www from the url](https://forum.nginx.org/read.php?2,176137,176137#msg-176137)**
>
> Hello everyone, I used to use the following trick to cut the initial www. from the address so that www.mysite.com would redirect to mysite.com: \[code\] server { server\_name mysite.com www.mysite.com; if ($host ~\* www\\..\*) {

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 3:48pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/2 "2011-02-17T15:48:36Z")

</div>

On Thu, Feb 17, 2011 at 9:44 PM, whiskybar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why is that and more importantly, what trick should I use to redirect  
> [www.mysite.com](http://www.mysite.com) → [mysite.com](http://mysite.com)?

If I remember correctly:

server {  
server\_name [www.mysite.com](http://www.mysite.com);  
rewrite ^ $schema://mysite.com$request\_uri permanent;  
}

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 3:49pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/3 "2011-02-17T15:49:14Z")

</div>

On 2/17/11 9:44 AM, whiskybar wrote:

> ```
> rewrite ^(.*)$ $1 permanent;
> }
> 
> ```
> 
> [/code]

Wrong way!

server {  
server\_name [mysite.com](http://mysite.com);  
…  
}

server {  
server\_name [www.myite.com](http://www.myite.com);  
rewrite ^ http://mysite.com$request\_uri? permanent;  
}

> header, you can use a wildcard “\*” server\_name (but see the restrictions
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 3:52pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/4 "2011-02-17T15:52:22Z")

</div>

On Thu, Feb 17, 2011 at 3:47 PM, Edho P Arief [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> If I remember correctly:
> 
> server {  
> server\_name [www.mysite.com](http://www.mysite.com);  
> rewrite ^ $schema://mysite.com$request\_uri permanent;  
> }

Exactly what I was about to suggest. Seems to me like you do remember  
correctly.

Regards,  
Luit van Drongelen

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 4:09pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/5 "2011-02-17T16:09:22Z")

</div>

## edogawaconan Wrote:

> If I remember correctly:
> 
> server {  
> server\_name [www.mysite.com](http://www.mysite.com);  
> rewrite ^ $schema://mysite.com$request\_uri  
> permanent;  
> }

Good memory. Not “$schema” though, but “$scheme”

```auto
server {
  server_name www.mysite.com;
  rewrite ^ $scheme://mysite.com$request_uri permanent;
}
server {
  server_name mysite.com;
  ...
}
```

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

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 4:10pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/6 "2011-02-17T16:10:39Z")

</div>

Thank you all. Your solutions seem quite elegant.

Just a note, I was hoping for something I can do inside the server only  
declaration, just like I can do with the opposite redirection [mysite.com](http://mysite.com)  
 → [www.mysite.com](http://www.mysite.com):

```auto
server {
    server_name www.mysite.com mysite.com;

    if ($host !~* www\..*) {
        rewrite ^(.*)$ http://www.$host$1 permanent;
    }
```

On the other hand, I feel your solution with the two server declaration  
can even be a bit faster (not to mention easy to understand). Your  
solution suggests I should rewrite the above code into two server  
declarations as well.

Thank you very much!  
Jiri

Posted at Nginx Forum:

> **[Re: remove www from the url](https://forum.nginx.org/read.php?2,176137,176146#msg-176146)**
>
> Hello everyone, I used to use the following trick to cut the initial www. from the address so that www.mysite.com would redirect to mysite.com: \[code\] server { server\_name mysite.com www.mysite.com; if ($host ~\* www\\..\*) {

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 4:10pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/7 "2011-02-17T16:10:39Z")

</div>

On 2/17/11 10:03 AM, Dayo wrote:

> Good memory. Not “$schema” though, but “$scheme”  
> [/code]  
> Not to be _too_ pedantic, but unless you are using both http and https,  
> using $scheme is unnecessary. I only use it for such situations. Without  
> having read the code (not that I’d likely understand it anyway), and  
> Igor can comment on this, it would seem a waste.

> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,176137,176144#msg-176144](http://forum.nginx.org/read.php?2,176137,176144#msg-176144)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 4:16pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/8 "2011-02-17T16:16:55Z")

</div>

On 17.02.2011, at 18:08, whiskybar wrote:

> server\_name [www.mysite.com](http://www.mysite.com) [mysite.com](http://mysite.com);  
> declarations as well.  
> Yes, two servers are easy to understand and faster, because  
> nginx does not run regex for main site:  
> [Converting rewrite rules](http://nginx.org/en/docs/http/converting_rewrite_rules.html)

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 4:53pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/9 "2011-02-17T16:53:16Z")

</div>

On Thu, Feb 17, 2011 at 09:44:10AM -0500, whiskybar wrote:

Hi there,

By now you’ve seen the “approved” way to do what you want.

But it looks to me as if the behaviour you see is to be expected, based  
on the documentation you quote:

> "Note that if a redirect is relative (has no host part), then  
> when redirecting Nginx uses the “Host” header if the header match name  
> of server\_name directive

Host header is [www.mysite.com](http://www.mysite.com). server\_name includes [www.mysite.com](http://www.mysite.com). So  
you  
rewrite [http://www.mysite.com/abc](http://www.mysite.com/abc) to [http://www.mysite.com/abc](http://www.mysite.com/abc). It’s  
quite  
reasonable for a browser to report that this isn’t a useful redirect,  
exactly as Firefox does.

Other than a little bit of grammar, the documentation looks correct.

> Well, this method no longer works. I do not know when it stopped working

Most likely it was in 0.8.48, where the default value for  
server\_name\_in\_redirect was set to “off”.

That suggests that you could, if you wanted to avoid the  
“proper” way, keep your current configuration and add an explicit  
“server\_name\_in\_redirect on”.

If you included mention of the previous and current versions used,  
it might show that that analysis is incorrect.

All the best,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 5:17pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/10 "2011-02-17T17:17:06Z")

</div>

Hi Francis,

it must have been for my bad understanding of “if the header match name  
of server\_name directive”. I did not understand what “name of  
server\_name directive” was – of course, [mysite.com](http://mysite.com) matches the  
directive, you are right!

Then, it was the change of the server\_name\_in\_redirect’s default in  
0.8.48 in addition to that - as you said. I’m running 0.9.x everywhere  
and this is why it stopped working. I only did not notice.

That was a very clear explanation of you Francis, thank you. I’m  
definitely not using this as everyone here suggested against it and I  
will be using the two server solution.

Jiri

[mysite.com](http://mysite.com) matches the seconds word of server\_name and that means it  
matches the directive, you are right! Then, it

> host part), then  
> exactly as Firefox does.
> 
> All the best,
> 
> ## f
> 
> Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

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

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 18, 2011, 12:43am UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/11 "2011-02-18T00:43:28Z")

</div>

On Thu, Feb 17, 2011 at 03:51:33PM +0000, Francis D. wrote:

Hi there,

> But it looks to me as if the behaviour you see is to be expected, based  
> on the documentation you quote:

[from [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule#rewrite) ]

“”"  
Note that if a redirect is relative (has no host part), then when  
redirecting Nginx uses the “Host” header if the header match name of  
server\_name directive or the first name of server\_name directive, if  
the header does not match or is absent. If no server\_name is set, then  
the local hostname is used. If you want Nginx to always use the “Host”  
header, you can use a wildcard “\*” server\_name (but see the  
restrictions  
on doing so).  
“”"

> Other than a little bit of grammar, the documentation looks correct.

Actually, now that I look at it again and actually _test_, I believe  
this documentation is wrong – at least as it relates to 0.9.4.

Reality seems to be more like

“”"  
Note that if a redirect is relative (has no host part),  
then Nginx generates the host part based on the value of  
(./HttpCoreModule#server\_name\_in\_redirect) server\_name\_in\_redirect,  
which now defaults to “off”.  
“”"

That linked documentation says

“”"  
If server\_name\_in\_redirect is on, then Nginx will use the first value  
of the server\_name directive for redirects. If server\_name\_in\_redirect  
is off, then nginx will use the requested Host header.  
“”"

which is true, but an extra note like “if the first value of the  
server\_name directive used is not a plain hostname, then generated  
redirects are not likely to be useful” might warn people against blindly  
setting server\_name\_in\_redirect on.

Could someone with wiki access verify that and update the pages?

Cheers,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 18, 2011, 6:36pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/12 "2011-02-18T18:36:23Z")

</div>

server {  
listen 80;  
server\_name [test.co.uk](http://test.co.uk);  
rewrite ^ http://www.test.co.uk$request\_uri permanent;  
}

Posted at Nginx Forum:

> **[Re: remove www from the url](https://forum.nginx.org/read.php?2,176137,176375#msg-176375)**
>
> Hello everyone, I used to use the following trick to cut the initial www. from the address so that www.mysite.com would redirect to mysite.com: \[code\] server { server\_name mysite.com www.mysite.com; if ($host ~\* www\\..\*) {

---

<div class="post-metadata">

**Author:** ![whiskybar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/50afbb/32.png) [@whiskybar](https://www.ruby-forum.com/u/whiskybar)\
**Post date:** [February 17, 2011, 4:35pm UTC](https://www.ruby-forum.com/t/remove-www-from-the-url/203305/13 "2011-02-17T16:35:45Z")

</div>

Jim O. Wrote:

> Not to be _too_ pedantic, but unless you are using  
> both http and https,  
> using $scheme is unnecessary. I only use it for  
> such situations. Without  
> having read the code (not that I’d likely  
> understand it anyway), and  
> Igor can comment on this, it would seem a waste.  
> A bit too pedantic I think as you will note that the examples do not  
> include the “listen” directive … I.E. not sure if he is putting it  
> into the http or https sections so “$scheme” gives flexibility in  
> answering.

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