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.

I am trying to use SetEnvIf (Apache2, via a .htaccess file) to store an environment variable where the value to be assigned to the variable needs to contain spaces. For example, trying to set environment variable "AUTH_KEY" to have value "a b":

SetEnvIf Request_URI "^/example" AUTH_KEY="a\ b"

No matter what I do, I have not been able to escape the space in the value - the space acts as a delimiter between env vars to be created (in the above AUTH_KEY is created with value '"a\' and a second env var is created called 'b"'). I have tried single-quotes too with no luck.

How do I escape the space character?

share|improve this question
    
This is a terrible shot in the dark and I don't even expect it to work. What happens if you URLencode the space? (i.e. use %25 in place of one) Yes, I know this isn't a URL and I don't expect it to be treated like one. –  Andrew B Apr 4 '13 at 22:05
    
Thanks Andrew, in the end I figured out how to add spaces without having to use escaping (it works for me at least). I found that placing the quotes around the key=value, rather than just the value was the way to go. –  Alex B Apr 5 '13 at 13:52

2 Answers 2

up vote 1 down vote accepted

In the end I figured it out for myself…

SetEnvIf Request_URI "^/example" "AUTH_KEY=a b"

… which looks odd, but works. It correctly sets AUTH_KEY to value a b

Hopefully this might save someone else's time in the future.

The details:- I am not a C programmer, but bumbling around on Google I found mod_setenvif.c listed at http://www.bvbcode.com/code/s6148jvr-385031

A function is called on line 405 (ap_getword_conf, for which I found detail at http://docstore.mik.ua/orelly/apache_mod/155.htm) which seems to parse strings, delimited by whitespace (but optionally encapsulated in quotes and accepting the use of escape characters).

I noticed that this happened before the sub-string returned by the function above being split by the '=' character (line 411, by function ap_getword). Thus quotes around the key=value pair, rather than just the value.

share|improve this answer

Try using instead SetEnvIf Request_URI "^/example" AUTH_KEY="a\sb". \s here represents the space.

share|improve this answer
    
Thanks, I tried that after you posted but still no luck (the variable value gets set as "a\sb" (i.e. \s does not get replaced). –  Alex B Apr 4 '13 at 19:06
    
Also worth noting is that the quote marks are included in the value - they seem to be part of the value itself. Thanks –  Alex B Apr 4 '13 at 19:08
    
Try using a[\s]b. Looking forward to your results. –  Meriadoc Brandybuck Apr 4 '13 at 19:21
    
Still no luck, a[\s]b is set as the value as-is. Thanks for the thought though. –  Alex B Apr 4 '13 at 19:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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