# Rewrite or internal redirection cycle?

**URL:** <https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153>\
**Category:** NGINX\
**Created:** [May 19, 2011, 3:21pm UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153 "2011-05-19T15:21:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [May 19, 2011, 3:21pm UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153/1 "2011-05-19T15:21:37Z")

</div>

Hi,

I’m having a bit of an issue with the rewrite scheme:

server {  
…  
location / {  
try\_files $uri $uri/ @data;  
}

location @data {  
rewrite ^ /data.php$is\_args;  
internal;  
}  
…  
}

When I access this request:  
[http://domain.com/information/feedback/?order=desc&sort=date](http://domain.com/information/feedback/?order=desc&sort=date)

I get an internal redirection cycle:  
\*1 rewrite or internal redirection cycle while processing “/data.php?”,  
client: IP, server: [domain.com](http://domain.com), request: “GET  
/information/feedback/?order=desc&sort=date HTTP/1.1”, host:  
“[domain.com](http://domain.com)”, referrer: “[http://domain.com/information/feedback/](http://domain.com/information/feedback/)”

However, if I access this request:  
[http://domain.com/information/feedback/400028-some-information.html](http://domain.com/information/feedback/400028-some-information.html)

Everything works properly. What do you recommend to do, in order to  
troubleshoot the issue?  
My goal is to actually log what goes wrong with the php code, when the  
redirection cycle is encountered.

Thanks for your help.

Posted at Nginx Forum:

> **[Rewrite or internal redirection cycle?](https://forum.nginx.org/read.php?2,199260,199260#msg-199260)**
>
> Hi, I'm having a bit of an issue with the rewrite scheme: server { ... location / { try\_files $uri $uri/ @data; } location @data { rewrite ^ /data.php$is\_args; internal; } ... }

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [May 20, 2011, 7:35am UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153/2 "2011-05-20T07:35:00Z")

</div>

On Thu, May 19, 2011 at 09:20:55AM -0400, TECK wrote:

> location @data {  
> \*1 rewrite or internal redirection cycle while processing “/data.php?”,  
> redirection cycle is encountered.
> 
> Thanks for your help.

You should either add “break” to stop internal redirection cycle:

location @data {  
rewrite ^ /data.php$args break;  
}

or should define /data.php in try\_files

location / {  
try\_files $uri $uri/ /index.php?$args;  
}

or (my favorite) should define exactly what nginx should do in @data:

location @data {  
fastcgi\_pass …  
fastcgi\_param SCRIPT\_FILENAME /path/to/data.php;  
…  
}

The later way looks more complex at the first sight, but it creates  
independed locations and allows configuration to grow at very large  
scale.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [May 20, 2011, 8:40pm UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153/3 "2011-05-20T20:40:31Z")

</div>

Thank you Igor, I will use the SCRIPT\_FILENAME setup.

Posted at Nginx Forum:

> **[Re: Rewrite or internal redirection cycle?](https://forum.nginx.org/read.php?2,199260,199596#msg-199596)**
>
> Hi, I'm having a bit of an issue with the rewrite scheme: server { ... location / { try\_files $uri $uri/ @data; } location @data { rewrite ^ /data.php$is\_args; internal; } ... }

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [June 15, 2011, 3:24pm UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153/4 "2011-06-15T15:24:16Z")

</div>

if is evil. (and not needed)

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [June 15, 2011, 3:11pm UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153/5 "2011-06-15T15:11:54Z")

</div>

I get the same problem with TECK  
The difference is my homepage is work well, but when I tried to click a  
link on my homepage, it’s get me 500 Internal Server Error.

Here my configuration:

if (-f $document\_root/maintenance.html) {  
rewrite ^(.\*)$ /maintenance.html last;  
break;  
}

```
location / {
    error_page 404 = //index.php?q=$uri;
    try_files $uri $uri/ /index.php?q=$request_uri;
}

location /administrator/ {
    rewrite ^(.*)$ https://www.domain.com$1 permanent;
}

if ($request_uri ~* "com_login") {
    rewrite ^(.*)$ https://www.domain.com$1 permanent;
}

location /en/ {
    index index.php;
    error_page 404 = //index.php?q=$uri;
    if (!-e $request_filename) {
        rewrite ^/en/(.*)$ /en/index.php?$1 last;
        break;
    }
}

location /en/administrator/ {
    rewrite ^(.*)$ https://www.domain.com$1 permanent;
}

location ~ \.php {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME

```

$document\_root$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;

and some output from error\_log :

2011/06/15 19:41:54 [error] 1985#0: \*208 rewrite or internal redirection  
cycle while internal redirect to “[//index.php](https://index.php)”, client: 66.249.72.65,  
server: [www.domain.com](http://www.domain.com), request: “GET /agenda.html HTTP/1.0”, upstream:  
“fastcgi://127.0.0.1:9000”, host: “[www.domain.com](http://www.domain.com)”  
2011/06/15 19:41:55 [error] 1985#0: \*213 rewrite or internal redirection  
cycle while internal redirect to “[//index.php](https://index.php)”, client: 203.130.206.49,  
server: [www.domain.com](http://www.domain.com), request: “GET /diploma.html HTTP/1.0”, upstream:  
“fastcgi://127.0.0.1:9000”, host: “[www.domain.com](http://www.domain.com)”, referrer:  
“[http://www.domain.com/](http://www.domain.com/)”

Posted at Nginx Forum:

> **[Re: Rewrite or internal redirection cycle?](https://forum.nginx.org/read.php?2,199260,207142#msg-207142)**
>
> Hi, I'm having a bit of an issue with the rewrite scheme: server { ... location / { try\_files $uri $uri/ @data; } location @data { rewrite ^ /data.php$is\_args; internal; } ... }

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [June 15, 2011, 4:36pm UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153/6 "2011-06-15T16:36:41Z")

</div>

On 15 Jun 2011 14h11 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I get the same problem with TECK The difference is my homepage is  
> work well, but when I tried to click a link on my homepage, it’s get  
> me 500 Internal Server Error.
> 
> Here my configuration:

Your configuration suffers from a lot of mod\_rewrite influenced  
thinking. As Igor calls it, it’s a Yoda style of configuration.

This makes no sense. You’re doing a capture that you don’t use and  
you seem to be rewriting to the same URI.

> if (-f $document\_root/maintenance.html) {  
> rewrite ^(.\*)$ /maintenance.html last;  
> break;  
> }

location = /maintenance.html { }

location / {  
try\_files $uri $uri/ /index.php?q=$uri&$args;  
}

location /administrator/ {  
rewrite ^ https://www.domain.com$request\_uri? permanent;  
}

location ~\* com\_login {  
rewrite ^ https://www.domain.com$request\_uri? permanent

}

location /en/ {

location /en/administrator {  
rewrite ^ https://www.domain.com$request\_uri? permanent;  
}

location /en/(.\*)$ {  
index index.php;  
try\_files $uri $uri/ /en/index.php?$1;  
}  
}

> location ~ .php {  
> fastcgi\_pass 127.0.0.1:9000;  
> fastcgi\_index index.php;  
> fastcgi\_param SCRIPT\_FILENAME  
> $document\_root$fastcgi\_script\_name;  
> include /usr/local/nginx/conf/fastcgi\_params;

Instead of this insecure config, if the handler script is index.php do:

location = /index.php {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
}

— appa

---

<div class="post-metadata">

**Author:** ![teck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b2d939/32.png) [@teck](https://www.ruby-forum.com/u/teck)\
**Post date:** [June 15, 2011, 4:47pm UTC](https://www.ruby-forum.com/t/rewrite-or-internal-redirection-cycle/207153/7 "2011-06-15T16:47:12Z")

</div>

On 15 Jun 2011 15h33 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }

This can be made simpler I believe. Just

location /en/(.\*)$ {  
index index.php;  
try\_files $uri $uri/ /en/index.php?$1;  
}

should do the trick. Try it.

Also you’ll need to create another location for = /en/index.php.

location = /en/index.php {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
include /usr/local/nginx/conf/fastcgi\_params;  
}

— appa
