user1710825
user1710825

Reputation: 588

Check file exists with mod_rewrite

i want check if exist a file with "jpg" extension, in else check if "gif" exist, in else load a default image. All with mod_rewrite, i readed a lot of examples and all are the same. I want check a regex url. Now my httacces is this but not found:

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]

How i can resolve this?, Thanks.

Upvotes: 1

Views: 1700

Answers (3)

sameAuthorOfPost
sameAuthorOfPost

Reputation: 11

I resolved this with a solution of my friend, with 2 htaccess. In the main htacces i put:

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

In the second (inside profileImgs) this:

# 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

This allow me put other sizes... Seems extrange because use 2 htaccess and i think that will can make better, but this is the solution that i use now.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143856

Try:

# 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]

Upvotes: 2

Database_Query
Database_Query

Reputation: 634

try this

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

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

Upvotes: 0

Related Questions