0

I'm trying to use Environment Variables in Apache2 httpd.conf. The point would be to redirect port 80 to a https connection on same host and stay there.

I need it to get the HOSTNAME or HTTP_HOST as i don't previously know it. Using redirect or mod_rewrite is not the real question.

The problem is, I can't even output any of this variables. When I add %{HOSTNAME} or %{HTTP_HOST} and then browse to http://example.com/ it redirects (literally) to https://%{HOSTNAME}

I'm using CentOS, and probably missing something very important. Any help will be appreciated.

Edit 1: Listing Env Variables i can see HOSTNAME is defined, but HTTP_HOST is not there.

Doubt A: If I Define HTTP_HOST, what value should i give? Since I need it to output the current HTTP Host whenever it changes.

Doubt B: As suggested in documentation and other answers the correct sintax for this vars in httpd.conf would be (for example) %{HTTP_HOST}%{REQUEST_URI} I did use it this way, and tried also with HOSTNAME, expecting it to output the value instead of "%{HOSTNAME}" literally. If i have this one defined, why isn't it outputing the value?

Edit 2 SOLVED: Turned out HTTP_HOST wasn't working for this reason: https://stackoverflow.com/questions/10910541/is-the-http-host-server-variable-always-defined

As I am using IP Adress, because i still don't know what the HTTP_HOST will be.

Raul Cardoso
  • 35
  • 1
  • 8

1 Answers1

0

Are you sure that HTTP_HOST is defined?

Remember, these are shell vars, not the same as you would use in .htaccess

If apache cannot find the env variable you specified, it will leave the text as is.

The values of shell environment variables can be used in configuration file lines using the syntax ${ENVVAR}. If "ENVVAR" is the name of a valid environment variable, the value of that variable is substituted into that spot in the configuration file line, and processing continues as if that text were found directly in the configuration file. (If the ENVVAR variable is not found, the characters "${ENVVAR}" are left unchanged for use by later stages in the config file processing.)

http://httpd.apache.org/docs/2.2/configuring.html

I would recommend mod_rewrite to do this job, as it does it rather well.

David Houde
  • 3,200
  • 1
  • 16
  • 19
  • Thank you for your quick reply and recommendation. I have edited the question to add further details. Won't have access to this machine until monday, but i will keep an eye for further help and hints. – Raul Cardoso Mar 28 '13 at 21:05