Andy0708 Andy0708 - 1 year ago 146
Apache Configuration Question

Conditionally set headers in Apache htaccess

I have a PHP Laravel application with a standard

file as follows.

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

If the requested URL maps to a static file, it is served, and otherwise
is invoked. I need to add a set of headers for static files, which would kind of be the "else" of the last two conditions above. I need something like the below pseudo.

if static file
Header set MyHeaderOne "something"
Header set MyHeaderTwo "something"
end if

I can add the headers just fine, but got stuck at doing so conditionally. I looked a bit at
, which may make it possible.

So, how can I add some headers only for static files, i.e. if the requested file exists (e.g.
)? Thanks a lot in advance!

Answer Source

You can do a condition statement just like what you stated.

<If "%{REQUEST_URI} =~ /\.(gif|jpe?g|png|css|js)$/">
  #put your header set stuff here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download