Home > Net >  Reading environnement variables set from htacess inside php code
Reading environnement variables set from htacess inside php code

Time:12-24

I'm currently migrating a application from a very old symfony 2.1 version into a symfony 6 version.

And there is one piece of code that doesn't work anymore I cannot understand:

inside htaccess file there is multiple definitions like this one :

RewriteCond %{HTTP_HOST} ^www\.cg\.mywebsite(\.com)?(\:[0-9] )?$
RewriteRule (.*) $1 [E=COUNTRY:57]

Which says that if the host patterns follows the rewrite cond, then I can get my country value equals to 57 in my PHP code

$country = $request->server->get('COUNTRY', 1);

This works just fine in the old application, but in the new one I always get 1, as if country was not defined at all

Old application: Symfony 2.1 / Php 5

New application: Syfony 6 / Php 8

CodePudding user response:

the local server I'm using is the built in local symfony server.

.htaccess is an Apache config file. If you are using the built-in symfony web server (or the PHP built-in web server) then this is not Apache and the .htaccess file is not going to be processed (and the env vars are not going to be set).

If you need the .htaccess file to be processed for your application logic then you'll need to run Apache, or move this logic to your application/PHP.

  • Related