htaccess禁用特定URL的缓存

时间:2017-10-17 21:55:31

标签: apache .htaccess server

我使用.htaccess在我网站的前端缓存我的图像,JS和CSS文件。 该网站建立在Codeigniter 2下。

这是我的代码:

RewriteEngine on

#RewriteRule ^index\.php / [R=301]

RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index.php(?:/(.*))?$ https://www.mywebsite.com/$1 [R=301,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

#Compression Gzip
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/x-javascript application/javascript 
text/javascript text/css text/html text/xml
</IfModule>

# Cache One month for most static assets
<filesMatch ".(jpg|jpeg|png|gif|ico)$">
Header set Cache-Control "max-age=2628000, public"
</filesMatch>

#Force la redirection vers www.
#RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com$ [NC]
#RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
#RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
#RewriteRule .? https://www.mywebsite.com%{REQUEST_URI} [R=301,L]

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]




<IfModule mod_deflate.c>
   AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
   AddOutputFilterByType DEFLATE application/x-javascript
   AddOutputFilterByType DEFLATE application/xhtml+xml
   AddOutputFilterByType DEFLATE application/xml
   AddOutputFilterByType DEFLATE font/opentype
   AddOutputFilterByType DEFLATE font/otf
   AddOutputFilterByType DEFLATE font/ttf
   AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
   AddOutputFilterByType DEFLATE text/css
   AddOutputFilterByType DEFLATE text/html
   AddOutputFilterByType DEFLATE text/javascript
   AddOutputFilterByType DEFLATE text/plain
 </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>



 <IfModule mod_expires.c>
   ExpiresActive On
   ExpiresByType image/jpg "access 1 year"
   ExpiresByType image/jpeg "access 1 year"
   ExpiresByType image/gif "access 1 year"
   ExpiresByType image/png "access 1 year"
   ExpiresByType text/css "access 1 month"
   ExpiresByType text/html "access 1 month"
   ExpiresByType application/pdf "access 1 month"
   ExpiresByType text/x-javascript "access 1 month"
   ExpiresByType application/x-shockwave-flash "access 1 month"
   ExpiresByType image/x-icon "access 1 year"
   ExpiresDefault "access 1 month"
 </IfModule>
 <IfModule mod_headers.c>
   <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
   Header set Cache-Control "max-age=2678400, public"
   </filesmatch>
   <filesmatch "\.(html|htm)$">
   Header set Cache-Control "max-age=7200, private, must-revalidate"
   </filesmatch>
   <filesmatch "\.(pdf)$">
   Header set Cache-Control "max-age=86400, public"
   </filesmatch>
   <filesmatch "\.(js)$">
   Header set Cache-Control "max-age=2678400, private"
   </filesmatch>
 </IfModule>

工作正常。但是,当我在后端(www.mywebsite / admin)工作时,缓存也适用,这在更新图像和文本时非常烦人。

我在htaccess文件的最底部试过这个解决方案:
htaccess Disable cache if URL contains string?
但我的内容仍然是从内存缓存中读取的。

我还尝试了“Location”和“LocationMatch”指令,但它创建了一个内部服务器错误:

<Location  /admin>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</Location >

有人可以为此提供帮助吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

<Directory /some/real/path/to/tmp>
  Header Set Pragma "no-cache"
  Header Set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
  Header Set Cache-Control "max-age=0, no-store, no-cache, must-revalidate"
  Header Unset ETag
  FileETag None
</Directory>

这应该有效。