# POST redirect: different behavior in index.php vs included files

**URL:** <https://discourse.slimframework.com/t/post-redirect-different-behavior-in-index-php-vs-included-files/1810>\
**Category:** Questions\
**Created:** [September 12, 2017, 6:33pm UTC](https://discourse.slimframework.com/t/post-redirect-different-behavior-in-index-php-vs-included-files/1810 "2017-09-12T18:33:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skeagy](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@skeagy](https://discourse.slimframework.com/u/skeagy)\
**Post date:** [September 12, 2017, 6:33pm UTC](https://discourse.slimframework.com/t/post-redirect-different-behavior-in-index-php-vs-included-files/1810/1 "2017-09-12T18:33:45Z")

</div>

Any ideas on what could cause this first example to work and the second example to fail? I got as far as determining that the behavior depends only on where I stick the one line of code that triggers the redirect: it works in my main index.php file, and it fails when I put it at the end of the included code in another file. I’m using Apache webserver, I’ve got the files owned apache:apache and chmod 664, I changed the security context:  
sudo chcon -R system\_u:object\_r:httpd\_sys\_content\_t:s0 /var/www/secure/dev/{all my projects}

## Example 1: this works

within index.php:  
$app-\>post(’/add/template’, function ($request, $response, $args) {  
require\_once ‘…/app/routes/add\_template\_\_post.php’;  
**return $response-\>withStatus(303)-\>withHeader(‘Location’,$url);**  
});

within ./app/routes/add\_template\_\_post.php  
(some stuff)

## Example 2: this fails

within index.php:  
$app-\>post(’/add/template’, function ($request, $response, $args) {  
require\_once ‘…/app/routes/add\_template\_\_post.php’;  
});

within ./app/routes/add\_template\_\_post.php  
(some stuff - exacty the same)  
**return $response-\>withStatus(303)-\>withHeader(‘Location’,$url);**

---

<div class="post-metadata">

**Author:** ![skeagy](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@skeagy](https://discourse.slimframework.com/u/skeagy)\
**Post date:** [September 12, 2017, 6:43pm UTC](https://discourse.slimframework.com/t/post-redirect-different-behavior-in-index-php-vs-included-files/1810/2 "2017-09-12T18:43:41Z")

</div>

I should clarify what I mean by “it fails.”

What I observe is that my web browser does not redirect- just has empty content. My Apache access logs show a 200 OK response, and this is consistent with what I see using ‘ngrep’ to sniff the traffic from command line on the webserver.

Edit: I have confirmed this behavior change is repeatable with simply moving the location of a single line of code, from the last statement in an included file to the first line after the included file in the $app-\>get closure that calls it. Other commands in the included file are verified to run, so it is not a simple file permission or ownership thing.

---

<div class="post-metadata">

**Author:** ![rryyyk](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@rryyyk](https://discourse.slimframework.com/u/rryyyk)\
**Post date:** [May 8, 2018, 6:53am UTC](https://discourse.slimframework.com/t/post-redirect-different-behavior-in-index-php-vs-included-files/1810/4 "2018-05-08T06:53:48Z")

</div>

did you ever figure out what was happening? i have something similar,  
after i upgraded my slim version, redirects from within middleware result in the same output  
i get the no redirect behaviour with an empty response body

if you set your response to a var and write directly to the body i bet you will see it on the screen

> $response = $response-\>withStatus(303)-\>withHeader(‘Location’, $url);  
> $response-\>getBody()-\>write(‘foo’);  
> return $response;

what version of slim are you using? i think a bug may have been introduced somewhere above 3.8
