English 中文(简体)
如果文件存在, ht 访问重定向
原标题:.htaccess redirection if file exist
  • 时间:2012-05-24 05:34:39
  •  标签:
  • .htaccess

我有像这样的目录结构

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

基本上我想把我的 img URL 更改为父目录 (ub) ,

当我访问

首先,它必须检查 /ub/img/mypic.jpg (如果存在的话),然后将文件交给浏览器,如果不是,则将文件传送给(http://localhost/ub/index.php?img=mypic.jpg

第二,索引.php将创建 mypic.jpg 并将其存储到 /ub/img/ ,然后通过 mypic.jpg 到浏览器。

因此,无论文件是否存在,浏览器只能将http://localhost/ub/mypic.jpg 作为 URL

到目前为止,我在我的.htaccess档案里有这个

Options +FollowSymLinks
RewriteEngine on

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

我还有两天时间 找到解决办法 但似乎行不通

谢谢

<强 > EDIT:

嘿,我明白了,对 http://localhost/ub/app/file.php 的要求已经按照i的预期传送到 index.php?uri=ap/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]
最佳回答

尝试类似的东西( 将其放置在. httaccess / ub文件夹中) :

Options +FollowSymLinks
RewriteEngine on
RewriteBase /ub/

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

RewriteRule ^(.*)$ index.php?img=$1 
问题回答

暂无回答




相关问题
Ignore symlinks in clean URL s in .htaccess

Example URL: example.com/user /user is both a symlinked directory and a valid URL to content on my site. I user Horde Routes to request the content and all requests to the site go through index.php. ...

.htaccess File Options -Indexes on Subdirectories

I have the following .htaccess line, simple no indexes on root. Options -Indexes What do we add so it propagates to any sub directory instead of having to create one file for each? One .htaccess on ...

Mod Rewrite ignoring -d

After a day or two, I am still fighting with Mod Rewrite. It seems that 60% of my development time is spent battling the server. My current URL structure is such that all http://example.com/xyz and ...

ErrorDocument 404 Not Sending Referrer Information: PHP

I m using a standard htaccess ErrorDocument 404 to redirect users to a new page. In order to customize the 404, I need to know their referrer information (from the page they Tried to visit). $...

Find all htaccess files on server [closed]

I m looking for a Linux command to go through all the directories on my server and find all .htaccess files. The output would be a list of all those files with full path, creation/modification date ...

301 Redirect Help with Dynamic URL s and Multiple Id s

I just did a redesign for www.wildchildclothes.com which has a new URL structure. The old site ran on Zen Cart and had a bad URL structure. Here are some examples of what I want to redirect from: OLD:...