# Re: rewrite to www EXCEPT for one directory

**URL:** <https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714>\
**Category:** NGINX\
**Created:** [September 6, 2012, 10:33pm UTC](https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714 "2012-09-06T22:33:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [September 6, 2012, 10:33pm UTC](https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714/1 "2012-09-06T22:33:29Z")

</div>

> }  
> wbr, Valentin V. Bartenev

I suggest to use $scheme like in:

rewrite ^(.\*) $scheme://www.example.org/$1 permanent;

to keep the https connection (if a client wants to use it). otherwise  
your  
server drops the https connection and the client will connect over http.  
i.e.: if you want to use /wp-admin only over https with  
“define(‘FORCE\_SSL\_ADMIN’, true);” in /wp-config.php, it will safe you a  
lot of annoying behavior…

~ed

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 6, 2012, 11:35pm UTC](https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714/2 "2012-09-06T23:35:24Z")

</div>

On Friday 07 September 2012 00:32:48 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[…]

> I suggest to use $scheme like in:
> 
> rewrite ^(.\*) $scheme://www.example.org/$1 permanent;
> 
> to keep the https connection (if a client wants to use it). otherwise your  
> server drops the https connection and the client will connect over http.  
> i.e.: if you want to use /wp-admin only over https with  
> “define(‘FORCE\_SSL\_ADMIN’, true);” in /wp-config.php, it will safe you a  
> lot of annoying behavior…

“listen 80;” means that this server section doesn’t handle https  
connections.

And please do not use ugly rewrites instead of simple “return”, where  
it’s  
possible.

wbr, Valentin V. Bartenev

–

> **[nginx: donation](http://nginx.org/en/donation.html)**

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 7, 2012, 2:13am UTC](https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714/3 "2012-09-07T02:13:49Z")

</div>

On Fri, Sep 7, 2012 at 5:51 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> maybe it’s a stupid question, but: if ‘permanent - returns permanent  
> redirect with code 301’ ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule)), then  
> why one should use ‘return 301 URL’ instat?

I believe a “return 301 URL” doesn’t do regexp processing compared to  
rewrite.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 7, 2012, 2:14am UTC](https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714/4 "2012-09-07T02:14:59Z")

</div>

On Friday 07 September 2012 02:51:35 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > On Friday 07 September 2012 00:32:48 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > And please do not use ugly rewrites instead of simple “return”, where  
> > it’s possible.
> 
> maybe it’s a stupid question, but: if ‘permanent - returns permanent  
> redirect with code 301’ ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule)), then  
> why one should use ‘return 301 URL’ instat?

Because the “return” directive is a simple construction that doesn’t  
involve  
execution of regular expressions, and looks more declarative.

Moreover, you do not need to capture full URI, when you have variables  
like $uri  
and $request\_uri. Therefore, something like ^(.\*)$ is just a waste of  
resources.

> or its more about to avoid rewriting rules generaly? if yes, why is this  
> so? a few weeks ago, i followed a discussion about to avoid if-rules  
> because of performance with large rule sets. but i can’t see this argument  
> with one rewriting rule, which only takes action at first connect from  
> client to server.

This is less about performance, but rather it’s more about keeping  
config clean  
and simple.

> it’s just a little bit confusing for me 'cause of so many poorly writen  
> config examples around the web, and it’s the first time i see this way  
> with ‘return 301 URL’ for redirecting [example.org](http://example.org) to [www.example.org](http://www.example.org).

So many examples and tutorials around the web have written by guys who  
have little understanding of the subject. And often they just copy-paste  
configuration from each other.

But in this particular case, it is worth to bear in mind, that early  
versions of nginx doesn’t support ‘return 30x URL’.

See the official documentation for details: [Module ngx\_http\_rewrite\_module](http://nginx.org/r/return)

wbr, Valentin V. Bartenev

–

> **[nginx: donation](http://nginx.org/en/donation.html)**

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 7, 2012, 12:51am UTC](https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714/5 "2012-09-07T00:51:59Z")

</div>

> On Friday 07 September 2012 00:32:48 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And please do not use ugly rewrites instead of simple “return”, where it’s  
> possible.
> 
> wbr, Valentin V. Bartenev

maybe it’s a stupid question, but: if ‘permanent - returns permanent  
redirect with code 301’ ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule)), then  
why one should use ‘return 301 URL’ instat?

or its more about to avoid rewriting rules generaly? if yes, why is this  
so? a few weeks ago, i followed a discussion about to avoid if-rules  
because of performance with large rule sets. but i can’t see this  
argument  
with one rewriting rule, which only takes action at first connect from  
client to server.

it’s just a little bit confusing for me 'cause of so many poorly writen  
config examples around the web, and it’s the first time i see this way  
with ‘return 301 URL’ for redirecting [example.org](http://example.org) to [www.example.org](http://www.example.org).

~ed

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 7, 2012, 2:20pm UTC](https://www.ruby-forum.com/t/re-rewrite-to-www-except-for-one-directory/222714/6 "2012-09-07T14:20:37Z")

</div>

On 7 Set 2012 02h13 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Fri, Sep 7, 2012 at 5:51 AM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > maybe it’s a stupid question, but: if ‘permanent - returns  
> > permanent redirect with code 301’  
> > ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule)), then why one should use  
> > ‘return 301 URL’ instat?
> 
> I believe a “return 301 URL” doesn’t do regexp processing compared  
> to rewrite.

You generate the variable/group on the location and then you can use  
it on the return statement. Very rarely is there a need to use a  
rewrite IMHO. I think it has more to do with the (bad) habits that  
mod\_rewrite promotes. That’s where we came from.

YMMV,  
— appa
