3

I'm setting an environment variable in an htaccess file and it's being prepended with "REDIRECT_". As far as I've read, this is caused by URL rewriting.. the thing is, I'm not doing any rewriting (that I'm aware of).

My webspace contains two files:

.htaccess

SetEnv Foo "bar"

index.php

<?php
print_r($_ENV);

Now, I'm guessing this may have something to do with the fact that this is on a shared hosting package with 1&1.. is there anything else I should be checking or has anyone experienced this before? Or am I just missing something??

Cheers

user1187347
  • 318
  • 3
  • 12

2 Answers2

4

The issue is triggered by using PHP as a CGI Wrapper. If PHP is running as mod_php apache module it's not prefixing your variables.

Reason for that is internal redirect handling thus apache recreates the variables with the REDIRECT_ prefix :-/

Solution (updated)

Use PHP-FPM or mod_php

If you wanna use CGI Wrapping for PHP put this into your .htaccess:

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Community
  • 1
  • 1
Rico Neitzel
  • 1,086
  • 9
  • 9
  • I am running with Apache 2.4 + mod_fastcgi + php-fpm with 'alias' directive to mapping to the external fastcgi path, I think this is the cause of my 'REDIRECT_' env var renaming, after switching to using 'mod_proxy_fastcgi', even with same .htaccess rule, the problem no longer exists. – weynhamz Dec 14 '17 at 04:21
  • Also, how to use php-fpm but no cgi wrapping? – weynhamz Dec 14 '17 at 04:22
0

First of all your syntax for SetEnv is wrong. It should be like this:

SetEnv Foo "bar"

Then to access this field from PHP you need to do:

echo $_SERVER["Foo"];

Which should show up as "bar".

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I tried changing the syntax but I'm getting the same results. $_SERVER['Foo'] is not set; instead I'm getting $_SERVER['REDIRECT_Foo'] (And ($_ENV['REDIRECT_Foo']) with the value of 'bar' – user1187347 Jan 15 '14 at 13:40
  • That could be due to some rules in your `httpd.conf/.htaccess`. If there is any existing code in your `.htaccess` then post it in your question. – anubhava Jan 15 '14 at 13:50
  • Well you posted code definitely has bad syntax as `SetEnv Foo="bar"` doesn't in `.htaccess` as I suggested in my answer. – anubhava Jan 15 '14 at 15:17
  • both SetEnv Foo="bar" and SetEnv Foo "bar" produce the same results – user1187347 Jan 15 '14 at 15:29
  • that's not correct as `SetEnv Foo="bar"` sets a variable with name `Foo="bar"` with no value but most likely in your case some rewrite is happening somewhere and prefixing `REDIRECT_` – anubhava Jan 15 '14 at 15:32
  • Yeah I've changed my htaccess file so that it has the correct syntax but I'm still getting the exact same results. I'm thinking it's probably got something to do with how 1&1's shared hosting works. – user1187347 Jan 15 '14 at 15:38