7

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?

2
  • 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, 2013 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, 2013 at 13:52

2 Answers 2

7

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.

0

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

4
  • 1
    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, 2013 at 19:06
  • 1
    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, 2013 at 19:08
  • Try using a[\s]b. Looking forward to your results. Apr 4, 2013 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, 2013 at 19:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .