htaccess rewrite if file not exists in cache folder

孤街醉人 提交于 2019-12-20 02:52:16

问题


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:


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]


来源:https://stackoverflow.com/questions/24586809/htaccess-rewrite-if-file-not-exists-in-cache-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!