# Subdirectory still redirecting to www. despite rule

**URL:** <https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016>\
**Category:** NGINX\
**Created:** [September 19, 2012, 2:26am UTC](https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016 "2012-09-19T02:26:43Z")\
**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 19, 2012, 2:26am UTC](https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016/1 "2012-09-19T02:26:43Z")

</div>

This issue is connected to the threads

rewrite to www EXCEPT for one directory  
([Rewrite to www EXCEPT for one directory - NGINX - Ruby-Forum](http://www.ruby-forum.com/topic/4405551)) and

Nginx location rule for Wordpress Multisite in subdirectories  
([Nginx location rule for Wordpress Multisite in subdirectories - NGINX - Ruby-Forum](http://www.ruby-forum.com/topic/4405180))

After making the changes to my nginx.conf. I reloaded the config. After  
going to [example.com/blogs/wp-admin/install.php](http://example.com/blogs/wp-admin/install.php) the browser still  
redirected to [www.example.com/blogs/wp-admin/install.php](http://www.example.com/blogs/wp-admin/install.php)

Here are the applicable lines in the conf:

server {  
server\_name [example.com](http://example.com);  
listen 80;

location / {  
return 301 http://www.example.com$request\_uri;  
}

location ^~ /blogs {  
try\_files $uri /blogs/index.php?q=$uri;  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:10004;  
}  
}  
if ($uri ~ (/blogs/[^/]\*)) {  
set $blogname $1;  
}  
location ^~ /blogs/$blogname {  
try\_files $uri $blogname/index.php?q=$uri;  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:10004;  
}  
}  
}

I’m wondering if it’s an error in combining the config advice for the  
two  
separate issues in the two threads.

Thanks.

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [September 19, 2012, 2:37am UTC](https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016/2 "2012-09-19T02:37:14Z")

</div>

Stop using a browser to test with and instead do curl -I  
Browsers cache, always.

Posted at Nginx Forum:

> **[Re: Subdirectory still redirecting to www. despite rule](https://forum.nginx.org/read.php?2,230832,230833#msg-230833)**
>
> This issue is connected to the threads rewrite to www EXCEPT for one directory (http://www.ruby-forum.com/topic/4405551) and Nginx location rule for Wordpress Multisite in subdirectories (http://www.ruby-forum.com/topic/4405180) After making the cha

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [September 19, 2012, 3:05am UTC](https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016/3 "2012-09-19T03:05:52Z")

</div>

On Tue, September 18, 2012 8:36 pm, Ensiferous wrote:

> Stop using a browser to test with and instead do curl -I  
> Browsers cache, always.

a) I had never been to the URL before and b) I had cleared the browser’s  
cache before testing.

running curl -I I get:

HTTP/1.1 200 OK  
Server: nginx/1.2.2  
Date: Wed, 19 Sep 2012 00:53:36 GMT  
Content-Type: text/html  
Connection: keep-alive

In Firefox with cache cleared, I get the redirect to the WP install page  
on [www.example.com](http://www.example.com) instead of staying at [example.com](http://example.com)

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [September 19, 2012, 10:01am UTC](https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016/4 "2012-09-19T10:01:52Z")

</div>

On 19/09/2012 3:21 AM, Valentin V. Bartenev wrote:

> > }  
> > But your example config is completely broken. Probably you do not understand how  
> > “location” works. If so, you should read these articles:
> 
> [How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html)  
> [Module ngx\_http\_core\_module](http://nginx.org/r/location)
> 
> > location ^~ /blogs/$blogname {
> 
> Variables are not allowed here.

As it turns out, the issue was a missing document root (since I  
previously only used that server section to redirect to www) Added the  
root and it worked fine.

As for the variable in the location, I did not write that. It was  
suggested to me in the thread:

[http://www.ruby-forum.com/topic/4405180#1073498](http://www.ruby-forum.com/topic/4405180#1073498)

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [September 19, 2012, 9:22am UTC](https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016/5 "2012-09-19T09:22:05Z")

</div>

On Wednesday 19 September 2012 04:25:53 Ian M. Evans wrote:  
[…]

> location ^~ /blogs {  
> try\_files $uri /blogs/index.php?q=$uri;  
> location ~ .php$ {  
> fastcgi\_pass 127.0.0.1:10004;  
> }  
> }  
> if ($uri ~ (/blogs/[^/]\*)) {  
> set $blogname $1;  
> }

location /blogs {

```
  ...

  location ~ ^/blogs/([^/]+) {
     ...
  }

```

}

But your example config is completely broken. Probably you do not  
understand how  
“location” works. If so, you should read these articles:

[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html)  
[Module ngx\_http\_core\_module](http://nginx.org/r/location)

> location ^~ /blogs/$blogname {

Variables are not allowed here.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![Ian\_MSEvans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/7bcc69/32.png) [@Ian\_MSEvans](https://www.ruby-forum.com/u/Ian_MSEvans)\
**Post date:** [September 19, 2012, 11:02am UTC](https://www.ruby-forum.com/t/subdirectory-still-redirecting-to-www-despite-rule/223016/6 "2012-09-19T11:02:41Z")

</div>

On 19 Set 2012 10h01 CEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

On 19/09/2012 3:21 AM, Valentin V. Bartenev wrote:

> > }
> 
> location /blogs {
> 
> ```
> ...
> 
> location ~ ^/blogs/([^/]+) {
> ...
> }
> 
> ```
> 
> }

The suggested config of adding a variable is over complicated and very  
little Nginxy. That’s seems like a throwback to Apache’s style IMO.

Valentin’s suggestion with blogname being a capture in a regex based  
(nested) location is a much cleaner way. It’s also informative in the  
way Nginx configuration language works. It’s mostly declarative.

Try it out.

— appa

> 

As it turns out, the issue was a missing document root (since I  
previously only used that server section to redirect to www) Added the  
root and it worked fine.

As for the variable in the location, I did not write that. It was  
suggested to me in the thread:

[http://www.ruby-forum.com/topic/4405180#1073498](http://www.ruby-forum.com/topic/4405180#1073498)

* * *

nginx mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[mailman.nginx.org archive](http://mailman.nginx.org/pipermail/)**
