how to use rewrite, if first rewrite doesn't yield a result (search in different locations)

試著忘記壹切 提交于 2019-12-14 03:27:19

问题


I use the following rewrite rule to retrieve files from a different directory and it works nicely:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((ffmpeg|ffprobe|ffserver|ffplay)-[0-9]+-g[0-9a-z]+\.(?:7z|dmg))$  /pub/$2/snapshots/$1  [L]

However, only 30 files are kept in /pub/$2/snapshots/$1 and older files are moved to a different "archive" directory (e.g. /pub/archive/$1).

Is there a way to rewrite to /pub/archive/$1, if the file isn't available in /pub/$2/snapshots/$1 anymore?


回答1:


What no one has pointed out to you is that, as per the docs, RewriteConds accept backreferences to the matched pattern in the RewriteRule. So where you are currently testing that the request is not a regular file you would instead check your preferred substitution is a file. Repeat this for your fallback substitution.


Edit: the RewriteConds in your comment don't make sense, the first part should be the subject of the test, the second part the test. You put the backreferences to the rule in the subject. Obviously, you've had a go, try this:

RewriteCond /pub/$1/snapshots/$0 -f
RewriteRule ^(ffmpeg|ffprobe|ffserver|ffplay)-[0-9]+-g[0-9a-z]+\.(?:7z|dmg)$  /pub/$1/snapshots/$0  [END]
RewriteCond /pub/archive/$0 -f
RewriteRule ^(?:ffmpeg|ffprobe|ffserver|ffplay)-[0-9]+-g[0-9a-z]+\.(?:7z|dmg)$  /pub/archive/$0  [END]


来源:https://stackoverflow.com/questions/50325200/how-to-use-rewrite-if-first-rewrite-doesnt-yield-a-result-search-in-different

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