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 try to get the value of the request parameter "authorization" and to store it in the header "Authorization" of the request.

The first rewrite rule works fine. In the second rewrite rule the value of $2 does not seem to be stored in the environement variable. As a consequence the request header "Authorization" is empty.

Any idea ? Thanks.

<VirtualHost *:8010>
  RewriteLog "/var/apache2/logs/rewrite.log"
  RewriteLogLevel 9
  RewriteEngine On
  RewriteRule ^/(.*)&authorization=@(.*)@(.*) http://<ip>:<port>/$1&authorization=@$2@$3 [L,P]
  RewriteRule ^/(.*)&authorization=@(.*)@(.*) - [E=AUTHORIZATION:$2,NE]
  RequestHeader add "Authorization" "%{AUTHORIZATION}e"
</VirtualHost>

I need to handle several cases because sometimes parameters are in the path and sometines they are in the query. Depending on the user. This last case fails. The header value for AUTHORIZATION looks empty.

# if the query string includes the authorization parameter
RewriteCond %{QUERY_STRING} ^(.*)authorization=@(.*)@(.*)$
# keep the value of the parameter in the AUTHORIZATION variable and redirect
RewriteRule ^/(.*) http://<ip>:<port>/ [E=AUTHORIZATION:%2,NE,L,P]
# add the value of AUTHORIZATION  in the header
RequestHeader add "Authorization" "%{AUTHORIZATION}e"
share|improve this question
    
Your editing has done something to the layout of the question, and I can't work out what you're trying to say. Is that an answer to the problem? Can you re-edit to get the layout a bite easier to read. –  CK. Aug 4 '09 at 13:58
add comment

3 Answers 3

It looks like you've got the 'L' (last) flag on the first rule. Rule processing will stop there, and no more re-writing will happen. I don't think the second rule will ever be reached. Try removing the 'L' flag.

Edit

Oh, and you've got the 'P' (proxy) flag set, too. That will also stop rewrite rule processing at that point and force a proxy request through mod_proxy.

Can you do it all in one rule, as the pattern match is the same. I'm not completely sure what you're after, but this might do it:

RewriteRule ^/(.*)&authorization=@(.*)@(.*) http://<ip>:<port>/$1&authorization=@$2@$3 [E=AUTHORIZATION:$2,NE,L,P]

Update

Aha, I think I see what you are trying to do now. As soon as you specify the [P] in the flags, the proxy request happens at that point. If I read the question correctly, you want the AUTHORIZATION var passed in to that request, so you'll need to put that in before the [P]:

# if the query string includes the authorization parameter
RewriteCond %{QUERY_STRING} ^(.*)authorization=@(.*)@(.*)$
RewriteRule ^/(.*) - [E=AUTHORIZATION:%2]
# add the value of AUTHORIZATION  in the header
RequestHeader add "Authorization" "%{AUTHORIZATION}e"
# keep the value of the parameter in the AUTHORIZATION variable and redirect
RewriteRule ^/(.*) http://<ip>:<port>/ [NE,L,P]

Completely untested, but should do what you want - if I understand the question correctly.

Apache Config Update

Do you have AllowOverride FileInfo set for that directory in httpd.conf? If not, then you won't be able to use RequestHeader in .htaccess

share|improve this answer
    
You understand the question correctly, but the solution doesnt seem to work. The %{AUTHORIZATION} variable still isnt set. –  Guillaume Aug 5 '09 at 7:28
    
Can you try some things to check to see what the problem is. The mod_headers docs suggest that "RequestHeader add Authorization %Authorization" might work. Can you try adding a fixed header to check that it's getting that far in the config. Something like "RequestHeader add DebugHeader DebugValue", and check that is getting through. –  CK. Aug 5 '09 at 10:36
add comment

I suspect you either can't override the Authorization header or that it's mangled later in the request process. As I'm sure you know, Authorization: is used for HTTP basic authentication so there's a good chance that something else is diddling it. Can you use a differently-named header?

share|improve this answer
add comment

Is there a reason you're using mod_rewrite for this and not mod_setenvif?

http://httpd.apache.org/docs/current/mod/mod_setenvif.html#setenvif

share|improve this answer
add comment

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.