在Apache htaccess中有条件地设置标头 [英] Conditionally set headers in Apache htaccess
问题描述
我有一个PHP Laravel应用程序,带有标准的 .htaccess
文件,如下所示。
I have a PHP Laravel application with a standard .htaccess
file as follows.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
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]
</IfModule>
如果请求的URL映射到静态文件,则将其提供,否则 index.php
被调用。我需要为静态文件添加一组标题,这将是上述最后两个条件的其他。我需要类似下面的伪代码。
If the requested URL maps to a static file, it is served, and otherwise index.php
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
设置MyHeaderTwo something
结尾我可以添加标题了,但是得到了坚持有条件地这样做。我看了一下 SetEnvIf
,这可能使之成为可能。
I can add the headers just fine, but got stuck at doing so conditionally. I looked a bit at SetEnvIf
, which may make it possible.
所以,我如何只添加一些标题对于静态文件,即是否存在请求的文件(例如 /images/example.jpg
)?
So, how can I add some headers only for static files, i.e. if the requested file exists (e.g. /images/example.jpg
)? Thanks a lot in advance!
推荐答案
您可以执行条件声明,就像您所说的一样。
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
</If>
https://httpd.apache.org/docs/2.4/expr.html#examples
这篇关于在Apache htaccess中有条件地设置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!