2009-12-07 40 views
9

我很好奇,如果有可能使用.htaccess來提供一個默認圖像,當一個特定的圖像被請求,不存在(注意:只有圖像請求應該引起這種行爲)。我知道我可以通過腳本來爲圖像提供服務,但是我更加好奇這是否可以使用.htaccess來完成。默認(回退)使用.htaccess的圖像?

假設我要求/thumbnails/010.gif,它不存在。我怎樣才能讓.htaccess代替/thumbnails/default.gif呢?

回答

10

我相信這會對你有用。把這個的.htaccess在縮略圖目錄及以下的任何的URI /不存在將直接縮略圖/thumbnails/default.gif

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
RewriteRule ^.*$ default.gif [NC,L] 
+3

看起來像它的伎倆。你能限制它只是調用圖像,而不是任何缺少的文件?也許(.png | .jpg | .gif | .jpeg) – Sampson 2009-12-07 05:57:53

2

因爲你只想把它應用到/縮略圖/

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^thumbnails/ thumbnails/default.gif 
1

如果你從這個目錄發送只有圖像,就可以利用ErrorDocument聲明是這樣的:

ErrorDocument 404 /thumbnails/default.gif 

/thumbnails必須與您的DocumentRoot相關。

我用FF測試過它,它按照承諾進行測試。但是,IE的「不顯示小於幾百KB的404s」可能會出現問題#%!$!