Apache的重写,如果文件不存在,请检查另一个目录 [英] Apache rewrite if file doesn't exist, check another directory

查看:462
本文介绍了Apache的重写,如果文件不存在,请检查另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到解决这一点,但只是还没有找到合适的例子对我的需要。

我在这类型的路径一些图片: /container/2011/08/myfile.ext 。我不得不将一些文件从该路径的一个是这样的: /container/newdir/2011/08/myfile.ext 。唯一的变化将是一些文件会移动到路径的NEWDIR的一部分,但该路径结构的其余部分是相同的。

我试图找出如何创建一个Apache / htaccess的重写,将首先检查在原来的位置文件 /container/2011/08/myfile.ext ,如果没有找到,请在新位置 /container/2011/08/myfile.ext 如果没有找到返回一个标准的404。

我发现过程和其他重定向解决方案,而不是整个路径。因为,当然,在容器中的项目有具体日期,我需要的东西,将采取的路径,被请求的文件,如果在请求位置没有发现,检查新的位置,如果没有找到,发送正常的404所以,它不只是一个单一的文件类型的故障转移的事,但更有活力。

任何人都可以阐明我怎么可能会去这个一些轻?


解决方案

 的RewriteCond%{REQUEST_URI} ^ /集装箱/ \\ D {4} / \\ D {2} /。
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^(容器)(/ \\ D {4} / \\ D {2} /.+)$ $ 1 / $ NEWDIR 2

如果 /container/2011/08/foo.bar 要求,但没有按'存在,它会重写 /集装箱/ NEWDIR / 2011/08 / foo.bar 。如果它不存在那里,它应该产生一个404。

\\ D 重新presents数字和 {X} 为重复X次。

I have tried to find a solution to this but just have not found the right examples for my need.

I have some images in this type of path: /container/2011/08/myfile.ext. I have to move some of the files from that path to one like this: /container/newdir/2011/08/myfile.ext. The ONLY change will be that some files will move to the "newdir" part of the path, but the rest of the path structure is the same.

I am trying to figure out how to create an Apache/htaccess re-write that will first check for the file in the original location /container/2011/08/myfile.ext, if not found, check in the new location /container/2011/08/myfile.ext if not found return a standard 404.

I have found solutions for process and other redirections, but not the entire path. Since, of course, the items in container are date specific, I need something that would take the path to the file being requested, if not found in requested location, check new location and if not found there, send the normal 404. So, it's not just a single file type failover, but something more dynamic.

Can anyone shed some light on how I might go about this?

解决方案

RewriteCond %{REQUEST_URI} ^/container/\d{4}/\d{2}/.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(container)(/\d{4}/\d{2}/.+)$ $1/newdir$2

If /container/2011/08/foo.bar is requested, but doesn' exist, it will rewrite to /container/newdir/2011/08/foo.bar. And if it doesn't exist there too, it should result in a 404.

\d represents a digit and {x} for repeating x times.

这篇关于Apache的重写,如果文件不存在,请检查另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆