2015-02-06 55 views
1

存在我有一个文件的结构是这样的:重写文件,如果在子目录

root 
|- public 
    |- file.jpeg 
    |- file.txt 
|- .htaccess 
|- index.php 

现在,我要重写example.com/file.jpegexample.com/public/file.jpeg与同为file.txt的和public目录中所有其他文件,但如果文件不子目录存在,则改写到index.php

出于某种原因,这个改写的.htaccess一切的index.php

RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}/public/$1 -f 
RewriteRule ^(.*)$ public/$1 [L] 

RewriteRule ^(.*)$ index.php [L] 

我的代码有什么问题?

回答

1

你错过了RewriteCond检查从最后的规则现有的文件/目录因此路由一切index.php

您可以使用:

RewriteEngine On 
RewriteBase/

# if direct access to /public needs be directed to index.php 
RewriteCond %{THE_REQUEST} \s/+public [NC] 
RewriteRule^index.php [L] 

RewriteCond %{DOCUMENT_ROOT}/public/$1 -f 
RewriteRule ^(.*)$ public/$1 [L] 

# if it is not /public/ then route to index.php 
RewriteRule !^public/ index.php [L,NC] 
+0

但随后在根的其他文件不被改写,这是我想避免的。 – vsakos 2015-02-06 23:23:44

+0

我想你不理解我。我想将/file.jpeg重写为/public/file.jpeg(如果存在),但是要将每个其他请求都写入index.php,甚至直接/public/file.jpeg。这可能吗? – vsakos 2015-02-07 11:30:12

+0

我需要这样的东西:http://pastebin.com/Uvptr6st – vsakos 2015-02-07 11:59:03