Reputation: 3208
I need a mod_rewrite rule and I can't figure it out myself.
The request URL can be:
https://image.domain.com/sub.domain.com/400x400/image.jpg
In .htaccess
I want to check if the following file exists:
/sub.domain.com/cache/400x400/image.jpg
If this file exists, than it should serve the file. If not exist, then continue to the next mod_rewrite rule.
Is this possible with mod_rewrite? If yes, how?
Upvotes: 1
Views: 778
Reputation: 786031
Create a sub.domain.com/.htaccess
if it doesn't exist with this code:
RewriteEngine On
# ignore files and directories from all rewrite rules
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule (^|/)index\.php$ - [L,NC]
RewriteCond %{DOCUMENT_ROOT}/sub.domain.com/cache/$1 -f
RewriteRule ^(?!cache/)(.+\.\w+)$ cache/$1 [L,NC]
#Else go to controller
RewriteRule ^(|(.*)/)([0-9]+x[0-9]+|[a-z]+[0-9]+|original)/(.*)$ ../index.php?path=$4&format=$3&folder=sub.domain.com [L,QSA]
Explanation:
(?!cache/)
- negative lookahead to ignore requests that already have cache/
(.+\.\w+)
- Match any request with at least an extension and captures it in $1
RewriteCond
that checks if request is NOT for an existing file or directoryRewriteCond
checks presence of $1
file using full path in cache/
directorycache/
Upvotes: 1