2012-10-01 184 views
1

我想檢查是否存在一個帶有「jpg」擴展名的文件,否則檢查是否存在「gif」,否則加載默認圖像。所有與mod_rewrite,我收穫了很多例子,都是一樣的。我想檢查正則表達式的URL。現在我的httacces是這個,但沒有找到:檢查文件是否存在與mod_rewrite

RewriteEngine On 
#RewriteBase/

# Do not do anything for already existing files and folders 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule .+ - [L] 

# add .html file extension (if such file does exist) 
# jpg 
RewriteCond %{DOCUMENT_ROOT}/profileImgs/$1_50\.jpg$ -f 
RewriteRule ^([0-9]+)/img/normal$ profileImgs/$1_50.jpg [L] 

# gif 
RewriteCond %{DOCUMENT_ROOT}/profileImgs/$1_50\.gif$ -f 
RewriteRule ^([0-9]+)/img/normal$ profileImgs/$1_50.gif [L] 

# default 
RewriteRule ^([0-9]+)/img/normal$ profileImgs/noImg_50.gif [L] 

我怎麼能解決這個問題?,謝謝。

回答

0

試試這個

RewriteCond %{DOCUMENT_ROOT}/profileImgs/$1_50\.jpg$ -f 
RewriteRule ^profileImgs/[^/\.]+$ cache/gif$ [L,QSA] 

RewriteRule ^profileImgs//([^/\.]+)$ profileImgs/.gif$ [L,QSA] 
2

嘗試:

# add .html file extension (if such file does exist) 
# jpg 
RewriteCond %{REQUEST_URI} ^/([0-9]+)/img/normal$ 
RewriteCond %{DOCUMENT_ROOT}/profileImgs/%1_50\.jpg$ -f 
RewriteRule ^([0-9]+)/img/normal$ profileImgs/$1_50.jpg [L] 

# gif 
RewriteCond %{REQUEST_URI} ^/([0-9]+)/img/normal$ 
RewriteCond %{DOCUMENT_ROOT}/profileImgs/%1_50\.gif$ -f 
RewriteRule ^([0-9]+)/img/normal$ profileImgs/$1_50.gif [L] 

# default 
RewriteRule ^([0-9]+)/img/normal$ profileImgs/noImg_50.gif [L] 
1

我解決了這個和我的朋友的溶液中,用2個htaccess的。 在主htacces我把:

RewriteRule ^([0-9]+)/normal profileImgs/$1/small [L,NS] 
#RewriteRule ^([0-9]+)/normal profileImgs/$1/otherSize [L,NS] 

在第二個(內profileImgs)這樣的:

# JPG 
RewriteCond %{REQUEST_FILENAME}_small.jpg -f 
RewriteRule ([0-9]+)/normal $1_small.jpg [NS,L] 

# GIF 
RewriteCond %{REQUEST_FILENAME}_small.gif -f 
RewriteRule ([0-9]+)/normal $1_small.gif [NS,L] 

# ANY 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ([0-9]+)/normal noImg_small.jpg 

# JPG 
#RewriteCond %{REQUEST_FILENAME}_otherSize.jpg -f 
#RewriteRule ([0-9]+)/normal $1_otherSize.jpg [NS,L] 

# GIF 
#RewriteCond %{REQUEST_FILENAME}_otherSize.gif -f 
#RewriteRule ([0-9]+)/normal $1_otherSize.gif [NS,L] 

# ANY 
#RewriteCond %{REQUEST_FILENAME} !-f 
#RewriteRule ([0-9]+)/normal noImg_otherSize.jpg 

這讓我把其他的尺寸......似乎交流中心,因爲使用2 htaccess的,我認爲這會做得更好,但這是我現在使用的解決方案。