.htaccess重定向,如果文件存在

时间:2012-05-24 05:34:39

标签: .htaccess

我有一个像这样的目录结构

/ub/
/ub/img/
/ub/inc/

基本上我想将img网址更改为其父目录(ub)

当我访问http://localhost/ub/mypic.jpg

首先,它必须检查/ub/img/mypic.jpg,如果存在,则获取该文件并传递给浏览器,如果没有,则将其传递给http://localhost/ub/index.php?img=mypic.jpg并进行内部重定向。

其次,index.php将创建mypic.jpg并将其存储到/ub/img/,然后将mypic.jpg传递给浏览器。

因此,无论文件是否存在,浏览器都只有http://localhost/ub/mypic.jpg作为网址

到目前为止,我在.htaccess文件中有这个

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/ub/img/$1 -f
RewriteRule .+ /ub/img/$1 [L]

我有2天时间找到解决方案,但是,它似乎不起作用,

谢谢,

编辑:

嘿,我有这个,http://localhost/ub/app/file.php的请求已按照我的预期传递给index.php?uri=app/file.php

但是http://localhost/ub/img/mypic.jpg的请求未传递给index.php?uri=img/mypic.jpg。有没有解决方案?

Options -MultiViews +FollowSymLinks 
RewriteEngine On
RewriteBase /ub/

RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/img/%1 -f
RewriteRule ^(.+)$ img/%1 [L]

RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/inc/%1 -f
RewriteRule ^(.+)$ inc/%1 [L]

RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]

1 个答案:

答案 0 :(得分:0)

尝试这样的事情(将它放在/ ub文件夹中的.htaccess中):

Options +FollowSymLinks
RewriteEngine on
RewriteBase /ub/

RewriteCond %{REQUEST_URI} ([^/]+\.jpg)$
RewriteCond %{DOCUMENT_ROOT}/ub/img/%1 -f
RewriteRule .+ - [L]

RewriteRule ^(.*)$ index.php?img=$1 
相关问题