2015-02-06 59 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