1

In an Apache server, I need to disable keepalive if a specific URL is matched.
To do so, I used mod_rewrite to set it then let the script run its course.

RewriteCond %{REQUEST_URI} ^/specific_url
RewriteRule  ^ - [E=nokeepalive]
.......
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

Now my problem is that nokeepalive end up being prefixed/renamed to with REDIRECT_ if it matches
From this question : When setting environment variables in Apache RewriteRule directives, what causes the variable name to be prefixed with "REDIRECT_"?
I assumed it was due to the RewriteRule
But it is not prefixed if I remove the RewriteCond (which would disable keepAlive globaly with any URL)
Is there a way to prevent this prefixing with RewriteCond ?
I cannot use SetEnvIf since I use mod_rewrite so I must use RewriteRule.

SetEnv/SetEnviF also won't work as they can not read from variable that were not assigned using SetEnv/SetEnvif. I tried with SetEnvIf to define only if REDIRECT_nokeepalive exist ( since nokeepalive just have to be defined, regardless of the value ) - but to no avail.

R.Damasinoro
  • 381
  • 2
  • 20

1 Answers1

1

I cannot use SetEnvIf since I use mod_rewrite so I must use RewriteRule.

You could use mod_setenvif with <If>.

<If "%{REQUEST_URI} =~ /regextomatchurl/">
# or use <If "%{REQUEST_URI} == 'urlstring'">
SetEnv nokeepalive 1
</If>

You can remove that rewrite rule.

OR:

You can just add this, it might work, not tested:

SetEnv nokeepalive ${REDIRECT_nokeepalive}

Edit:

Remember to add PassEnv REDIRECT_nokeepalive before SetEnv because SetEnv cannot read env variables from mod_rewrite.

Just to point out:

You have two spaces here, it could cause future problems in other RewriteRule(s):

RewriteRule  ^ - [E=nokeepalive]
           ^^
Example person
  • 3,198
  • 3
  • 18
  • 45
  • I used the second approach after setting it a first time. It's ugly but it works and I found no other workaround right now with that behaviour of Apache. – R.Damasinoro May 03 '21 at 13:03
  • The SetEnv nokeepalive ${REDIRECT_nokeepalive} won't work as SetEnv can not read from variable that were not assigned using SetEnv or SetEnvif. I tried with SetEnvIf to define only if REDIRECT_nokeepalive exist ( since nokeepalive just have to be defined, regardless of the value ) - but to no avail. – R.Damasinoro May 17 '21 at 09:07
  • @R.Damasinoro you can use `PassEnv REDIRECT_nokeepalive` before SetEnv – Example person May 17 '21 at 09:38
  • @R.Damasinoro, also you might need to change `${REDIRECT_nokeepalive}` to `%{REDIRECT_nokeepalive}e` – Example person May 17 '21 at 09:42