htaccess rewrite if file not exists in cache folde

2020-04-16 08:41发布

I want to check if file is not exist in the cache folder, than rewire it to a php file.

    RewriteCond %{DOCUMENT_ROOT}/cache/$0 !-f [NC]
    RewriteRule ^([^.]+\.(jpg|png|gif|css|js))$ include/cache/optimizer.php?file=$1&type=$2&c=$0 [L,QSA,NC]

eg1 : example.com/images/logo.jpg to check from example.com/cache/images/logo.jpg
eg2 : example.com/images/user/user1.jpg to check from example.com/cache/user/user1.jpg
eg3 : example.com/css/style.css to check from example.com/cache/css/style.css

Please help me in it.

thank you.

My htaccess code:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# Admin area
RewriteRule ^admin(.*) admin$1 [L]



# Rule for compression images and scripts
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI} !-f [NC]
RewriteRule ^([^.]+\.(jpg|png|gif|css|js))$ include/cache/optimizer.php?file=$1&type=$2&c=$0 [L,QSA,NC]



# Check if file *.* exist in the cache foldel
RewriteCond %{DOCUMENT_ROOT}/cache/$0 -f [NC]
RewriteRule ^(.+?)/?$ /cache/$0 [L]

# Check if file index.html  exist in the cache foldel
RewriteCond %{DOCUMENT_ROOT}/cache/$0/index.html -f [NC]
RewriteRule ^(.+?)/?$ /cache/$0/index.html [L]




# Normal behave
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

1条回答
混吃等死
2楼-- · 2020-04-16 08:55

You need to remove negative check ! and make it positive:

RewriteEngine On
RewriteBase / 
RewriteRule ^index\.php$ - [L] 

# Admin area 
RewriteRule ^admin(.*) admin$1 [L] 

# Check if file index.html exist in the cache foldel 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/cache/$1/index.html -f [NC] 
RewriteRule ^(.+?)/?$ /cache/$1/index.html [L] 

# Check if file *.* exist in the cache foldel 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/cache/$1 -f [NC] 
RewriteRule ^(.+?)/?$ /cache/$1 [L]

RewriteRule ^([^.]+\.(jpg|png|gif|css|js))$ include/cache/optimizer.php?file=$1&type=$2&c=$0 [L,QSA,NC] 

# Normal behave 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php [L]
查看更多
登录 后发表回答