htaccess的重写仅在文件不存在 [英] htaccess rewrite only if file doesn't exist
问题描述
使用的.htaccessmod-rewrite,我想重写URL,检查该文件是否存在,如果不使用原来的URL这是第一次重新编写过程中删除的部分再次改写到另一个URL。
Using .htaccess mod rewrite, I would like to rewrite a url, check if that file exists, and if not rewrite again to another url using part of the original url which was removed during the 1st re-write.
例如,如果原始网址是本
For example, if the original url is this
/images/3001/zebra.jpg
/images/3001/zebra.jpg
我想检查文件是否存在/images/cached/zebra.jpg和服务于它,如果它确实存在。
I would like to check if the file exists /images/cached/zebra.jpg and serve it if it does exist.
如果不是我想重写/image.php?id=3001
And if not I would like to rewrite to /image.php?id=3001
非常感谢, 菲尔
推荐答案
在你的文档根htaccess文件,之前无论规则,你可能已经有添加这些规则:
In the htaccess file in your document root, add these rules before whatever rules you may already have there:
RewriteEngine On
# cached copy exists
RewriteCond %{REQUEST_URI} ^/images/[0-9]+/(.+)$
RewriteCond %{DOCUMENT_ROOT}/images/cached/%1 -f
RewriteRule ^ /images/cached/%1 [L]
# cached copy doesn't exist
RewriteCond %{REQUEST_URI} ^/images/([0-9]+)/(.+)$
RewriteCond %{DOCUMENT_ROOT}/images/cached/%2 !-f
RewriteRule ^ /image.php?id=%1 [L]
这篇关于htaccess的重写仅在文件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!