简体   繁体   English

在Apache htaccess中有条件地设置标头

[英]Conditionally set headers in Apache htaccess

I have a PHP Laravel application with a standard .htaccess file as follows. 我有一个带有标准.htaccess文件的PHP Laravel应用程序,如下所示。

<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>

If the requested URL maps to a static file, it is served, and otherwise index.php is invoked. 如果请求的URL映射到静态文件,则将其提供,否则将调用index.php 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 SetEnvIf , which may make it possible. 我看了一下SetEnvIf ,这可能使之成为可能。

So, how can I add some headers only for static files, ie if the requested file exists (eg /images/example.jpg )? 那么,如何才能仅为静态文件添加一些头,即是否存在请求的文件(例如/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 https://httpd.apache.org/docs/2.4/expr.html#examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM