2014-03-31 58 views
0

我遇到了重寫問題。 這裏是我的.htaccess文件使用.htaccess忽略不存在路徑中的文件

RewriteEngine On 
RewriteBase/
#ignore non-existent utility-files 
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ 
RewriteCond %{REQUEST_FILENAME} !-f [OR] 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule .* - [L] 

#load existent utility files and don't rewrite them to index.php 
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ 
RewriteRule ^(.*) $1 [L] 
#load other files if they exist 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*) $1 [L] 
#load index.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php [L] 

最後一部分是工作,但我想避免當代碼請求非內已有的圖像(因爲我的index.php加載:www.mysite.test/images/foobar.png)

第一個RewriteCond-Line應該檢查請求是否與圖像,JavaScript等文件相匹配,第二個RewriteCond行應檢查該文件是否不存在。 第三個應該檢查文件夾是否存在。 如果比賽成功,應該加載任何東西。

它的工作原理,但只有當文件夾存在。如果該文件夾不存在,則不起作用。

任何幫助apreciated。在此先感謝

編輯:找到一個解決辦法 好吧,我得到了工作:

#<IfModule mod_php4.c> 
# php_value session.use_trans_sid 0 
#</IfModule> 
<IfModule mod_security.c> 
SecFilterEngine Off 
SecFilterScanPOST Off 
</IfModule> 
RewriteEngine On 
RewriteBase/

#load files if they exist 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*) $1 [L] 

#ignore non-existent utility-files 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ - [L] 

#load index.php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php [L] 

至關重要的一點是,如果目錄不存在我們提供了一個模式的重寫規則被忽視:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ - [L] 

回答

0

您有許多冗餘規則。你可以簡化爲:

RewriteEngine On 
RewriteBase/

#ignore non-existent utility-files 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt)$ [NC] 
RewriteRule^- [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php [L] 
+0

問題是,當PATH不存在時它不起作用 –

+0

哪個URL不起作用? – anubhava

+0

當它的圖像和PATH不存在時,它將轉到正常的404路 – anubhava

0

好吧,現有的文件仍然存在問題,正如成員anubhava所說。 我最終的解決方案是:

<IfModule mod_security.c> 
SecFilterEngine Off 
SecFilterScanPOST Off 
</IfModule> 
RewriteEngine On 
RewriteBase/

#load files if they exist 
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*) $1 [L] 

#ignore non-existent utility-files 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule \.(gif|jpe?g|s?html|css|js|cgi|png|ico|txt|php)$ - [L] 

#load index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php [L] 

該解決方案包括在第二重寫規則PHP-文件。因爲我有一些實際上是css文件並由瀏覽器加載的php文件。