.htaccess only if host name matches?

2019-07-26 08:56发布

问题:

I just setted up a local environament with same code that I had in production environament into a vagrant virtual machine with ubuntu32,

The thing is that I got Internal Server Error with all my rules in the .htaccess file

So I started removing separated blocks of code and this was the trouble:

# 1 weeks
<FilesMatch "\.(js|css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 1 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>

The thing is that I would like to use the exact same code for all environaments, is there a way I can only include that block of <filesMatch> only if not in localhost?

I found this answer but it just refers to a page, not the host name.

回答1:

You can probably do:

SetEnvIf Host ^ NON_LOCAL
SetEnvIf Host localhost !NON_LOCAL

<FilesMatch "\.(js|css)$">
Header set Cache-Control "max-age=604800, public" env=NON_LOCAL
</FilesMatch>

# 1 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public" env=NON_LOCAL
</FilesMatch>

# 1 MIN
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate" env=NON_LOCAL
</FilesMatch>