首页 > 解决方案 > Why does htaccess rewriteRule not work and returns 404 not found

问题描述

I'm trying to make simple image cacher

Idea is to request http://example.com/folder/00filename which is translated by .htaccess to http://example.com/folder/00/00filename.

If it does not exist, then call .php script.

PS: There is also ?url= part http://example.com/folder/00filename?url=http://s2.example.com/imagetocache.png which is needed by index.php

RewriteEngine on
RewriteBase /imagecache/
RewriteCond %{DOCUMENT_ROOT}/imagecache/$1/$2/$2$3 !-f #works
RewriteRule ^(.*)/(.{2})(.*)$ index.php [QSA,L] #works

RewriteCond %{DOCUMENT_ROOT}/imagecache/$1/$2/$2$3 -f #works
#RewriteRule ^(.*)/(.{2})(.*)$ dump.php [QSD,L] #works (if uncommented)
RewriteRule ^(.*)/(.{2})(.*)$ $1/$2/$2$3 [QSD,L] #does not work - returns 404 not found

Also, why these do not work? It's like ENV:REWRITEBASE is empty for RewriteCond but not for RewriteRule

RewriteCond %{DOCUMENT_ROOT}/%{ENV:REWRITEBASE}/$1/$2/$2$3 -f
RewriteCond %{ENV:REWRITEBASE}$1/$2/$2$3 -f
RewriteCond /$1/$2/$2$3 -f
RewriteCond $1/$2/$2$3 -f

标签: .htaccess

解决方案


推荐阅读