1

我只想检查 .htacess 文件的代码是否良好。我希望实现现场捕捉,所以我找到了代码并及时编辑了我的需要。

我希望缓存flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|swf文件 1 个月,gif|jpg|jpeg|png|js|css文件 2 周,xml|txt| htm|html文件 1 天,不缓存php|cgi|pl文件。

我需要的是这个:

  1. 代码没问题,还是其中可能有一些错误?

  2. 我的 .htacces 文件中是否需要同时使用 mod_headers.c 和 mod_expires.c,可以同时使用,也可以只使用一种?

这是 .htaccess 文件的代码:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault A86400        
    ExpiresByType image/x-icon A2592000
    ExpiresByType application/x-javascript A1209600
    ExpiresByType text/css A1209600
    ExpiresByType image/gif A1209600
    ExpiresByType image/png A1209600
    ExpiresByType image/jpeg A1209600
    ExpiresByType text/plain A86400
    ExpiresByType application/x-shockwave-flash A2592000
    ExpiresByType video/x-flv A2592000
    ExpiresByType application/pdf A2592000
    ExpiresByType text/html A86400
    </IfModule>

<ifmodule mod_headers.c>
## Turn on Expires and set default expires to 1 day
ExpiresActive On
ExpiresDefault A86400

## Set up caching on media files for 1 month
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|swf)$">
  ExpiresDefault A2592000
  Header append Cache-Control "public"
</FilesMatch>

## Set up caching on images css and js files for 2 weeks
<FilesMatch "\.(gif|jpg|jpeg|png|js|css)$">
  ExpiresDefault A1209600
  Header append Cache-Control "public"
</FilesMatch>

## Set up 1 day caching on commonly updated files
<FilesMatch "\.(xml|txt|htm|html)$">
  ExpiresDefault A86400 
  Header append Cache-Control "private, must-revalidate"
</FilesMatch>

## Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl)$">
  ExpiresDefault A0 
  Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
  Header set Pragma "no-cache"
</FilesMatch>
</ifmodule>

我也需要把这个

<ifModule mod_headers.c>
  Header unset ETag
</ifModule>
FileETag None

还有哪个更适合压缩使用:

<ifModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</ifModule>

或者

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
4

0 回答 0