在.htaccess
中设置了过期头(我们称之为主要的htaccess文件)。然后在这个文件夹的一个特定文件夹中我有另一个.htaccess
文件,因为我希望有一个例外,并且在那个子文件夹中的文件有不同的过期头。这个子文件夹包含test.min.css
。
然而,当测试它时,文件test.min.css
仍然有max-age=9072000
的过期头,我假设来自文件夹上的.htaccess
文件。
我在子文件夹中的.htaccess
文件中做错了什么?
主要的.htaccess
文件如下(它是一个WordPress站点):
#Expires headers configuration added by BREEZE WP CACHE plugin
<IfModule mod_env.c>
SetEnv BREEZE_BROWSER_CACHE_ON 1
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
# Assets
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
# Media assets
ExpiresByType audio/ogg "access plus 1 year"
ExpiresByType image/bmp "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/ogg "access plus 1 year"
ExpiresByType video/webm "access plus 1 year"
# Font assets
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType font/eot "access plus 1 year"
ExpiresByType font/opentype "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType application/font-woff2 "access plus 1 year"
# Data interchange
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/ld+json "access plus 0 seconds"
ExpiresByType application/schema+json "access plus 0 seconds"
ExpiresByType application/vnd.geo+json "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/rdf+xml "access plus 1 hour"
ExpiresByType application/atom+xml "access plus 1 hour"
# Manifest files
ExpiresByType application/manifest+json "access plus 1 week"
ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Favicon
ExpiresByType image/vnd.microsoft.icon "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 week"
# HTML no caching
ExpiresByType text/html "access plus 0 seconds"
# Other
ExpiresByType application/xhtml-xml "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/x-cross-domain-policy "access plus 1 week"
</IfModule>
#End of expires headers configuration
RewriteEngine On
RewriteRule ^.well-known/acme-challenge - [L]
# BEGIN Imagify: webp file type
<IfModule mod_mime.c>
AddType image/webp .webp
</IfModule>
# END Imagify: webp file type
# BEGIN Force https
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE]
# END Force https
# BEGIN Protect other domains trying to steal content
RewriteCond %{HTTP_REFERER} !^https://(www\.)? [NC]
RewriteCond %{HTTP_REFERER} !^https://(www\.)?.*$ [NC]
# END Protect content (incl scorm) to be accessed from other domains
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# MalCare WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
# END MalCare WAF
# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
# END Wordfence WAF
我在子文件夹中添加的.htaccess
文件:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 6 hours"
ExpiresByType text/css "access plus 6 hours"
</IfModule>
**更新:**我已经移除<IfModule mod_expires.c>
Package 。mod_expires
模块已启用/安装。完整的信头回应为:
HTTP/2 200 OK
server: nginx
date: Fri, 14 Oct 2022 12:06:43 GMT
content-type: text/css
last-modified: Tue, 11 Oct 2022 19:41:25 GMT
vary: Accept-Encoding
etag: W/"63***425"
cache-control: public, max-age=9072000
content-encoding: gzip
X-Firefox-Spdy: h2
1条答案
按热度按时间omqzjyyz1#
我仍然有
max-age=9072000
的到期头,我假设来自文件夹上的.htaccess
文件除了
9072000
seconds 是3.5个月(105天),这似乎也不对应于父.htaccess
文件中的任何内容。从这个请求的HTTP响应头到您的
.css
文件,似乎响应是由Nginx服务器提供的,而不是由Apache后端/应用程序服务器提供的。这意味着你在一个前端代理的后面,这个代理可能正在管理你的静态资产。它也负责你看到的缓存头。Apache(你的后端服务器)很可能被完全绕过来处理这样的请求。这是一个相对常见的优化,因为Nginx在服务静态资产方面性能更好。
实际上,仔细查看
.htaccess
文件后,我发现您正在检查“Force https”重定向中的X-Forwarded-Proto
HTTP响应头-这证实了您确实在(Nginx)代理服务器后面。(如果您没有这样做,则该规则将导致重定向循环,因为该头通常从不设置,除非由代理服务器设置。)如果是这种情况,那么您需要在代理服务器中设置一个例外,允许这些请求通过,以便由后端/Apache服务器处理。