Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache httpd: Conditionally set response header based on other *response* header

Tags:

apache

apache2

Is there a way to set a new response header conditionally, where the condition uses another response header? Specifically, the new response header should be set only if the response has a certain Content-Type.

I have looked into mod_headers in combination with mod_setenvif but it looks like conditions can only use request headers, not response headers.

Thanks, John

like image 347
ujay68 Avatar asked Feb 20 '14 09:02

ujay68


People also ask

How to set Apache conditional header based on url?

Using IF Else (Apache >=2.2) Similarly, if you want to set header based on requested url (e.g /product/) you can use QUERY_STRING variable. Just like, SetEnvIf, you can place this If-else condition in your server's location or directory directives, or . htaccess file.

Can we set response header?

You can set response headers, you can add response headers And you can wonder what the difference is. But think about it for a second, then do this exercise. Draw a line from the HttpResponse method to the method's behavior.

What does mod_ headers do?

This module provides directives to control and modify HTTP request and response headers. Headers can be merged, replaced or removed.


1 Answers

Apache 2.4 is the answer:

Set Cache-Control header when response content type is application/pdf

Header set Cache-Control "no-store,no-transform" "expr=%{resp:Content-Type} =~ m|application/pdf|"

Do NOT try with the IF directive. It is evaluated too early in the process. For example, the following will NOT work:

<If "%{resp:Content-Type} =~ m|application/pdf|"> Header set Cache-Control "no-store, no-transform" </If>

like image 75
Scott Avatar answered Oct 24 '22 14:10

Scott