2011-06-08 49 views
1

因此,我正在爲CMS編寫一個動態縮略圖生成器,我正在嘗試查找處理請求的最佳方式。具體來說,我需要提供一個htaccess文件的apache代碼,它將所有請求都送到縮略圖文件夾,並將它們發送到thumbnail.php文件。htaccess:轉發到PHP文件的特定目錄請求

理想情況下,它只會只有如果縮略圖不存在,則將您轉發到thumbnail.php,儘管檢查apache中的文件是否存在超出了我的範圍。

請求的URL應該是這樣的:

http://unknown_domain.com/unknown_folder/media/thumbnails/image-name.jpg?w=200&h=100&c=true

和圖像將被存儲在縮略圖像這樣(或者可能是相同的URL,如下如果這更有意義):

http://unknown_domain.com/unknown_folder/media/thumbnails/image-name-200-100-true.jpg

這是到目前爲止,我來最接近的,我以爲我把這個htaccess的在縮略圖文件夾中:

RewriteEngine on 
#RewriteBase /unknown_folder/media/thumbnails/ # optional, depends on your setup 

#Check for file existence here and forward if exists 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([a-z-])-([0-9]+)-([0-9]+)-true.jpg$ thumbnail.php?w=$2&h=$2&c=true [LQSE] 

感謝您提前提供任何幫助!

+1

你有什麼問題?你期待什麼樣的行爲?你實際上看到了什麼行爲? – KOGI 2011-06-08 17:14:21

回答

2

我猜你正在尋找一個正則表達式:

RewriteEngine on 
RewriteBase /unknown_folder/media/thumbnails/ # optional, depends on your setup 

#Check for file existence here and forward if exists 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([a-z-]+)-([0-9]+)-([0-9]+)-true\.jpg$ thumbnail.php?w=$2&h=$3&c=true [L,QSA] 

我沒有測試過,但最好到

/unknown_folder/media/thumbnails/bla-you-name-of-whatever-200-100-true.jpg 

的請求應該調用腳本thumbnail.php與它的參數。

修改:修復了一些錯別字和錯誤。

+0

謝謝,這正是我一直在尋找的。但是,當我嘗試訪問一個圖像時,它會給出一個內部服務器錯誤(500):'http:// getdirectus.com/dev/media/thumbnails/test-200-100-true.jpg' – RANGER 2011-06-08 17:29:16

+0

我認爲這是。 htaccess文件。檢查你的網絡服務器的錯誤日誌,應該說哪一行有錯誤。正則表達式錯誤也會顯示出來。 – hakre 2011-06-08 17:43:20

+2

點'.'需要轉義爲'\ .',並且標誌最後應該是'[L,QSA]'。 – anubhava 2011-06-08 17:43:47

0

不知道問題是什麼在這裏,但也有您應該使用來檢查文件是否存在幾個過濾器...

RewriteEngine on 

#Check for file existence here and forward if exists 
RewriteCond ^/(regex_here)/$ !-f 
RewriteCond ^/(regex_here)/$ !-d 
RewriteCond ^/(regex_here)/$ !-s 
RewriteCond ^/(regex_here)/$ !-l 

RewriteRule ^/(regex_here)/$ /thumbnail.php?w=$1&h=$2&c=$3 
相關問題