Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

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: http://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.

share|improve this question

marked as duplicate by Michael Hampton, Jenny D, mdpc, Khaled, Jay Mar 31 '13 at 22:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

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.

share|improve this answer
    
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.