2016-04-30 141 views
1

我正在使用codeigniter,並且在我的日誌文件中保留了這些404錯誤。Htaccess文件 - 在某些文件夾中找不到404頁面

ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js 
ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js 
ERROR - 2016-04-30 16:41:18 --> 404 Page Not Found: Assets/js 

因此,每個頁面加載都會在日誌文件中導致上述錯誤的一個條目。我的htaccess文件如下所示。這裏有什麼問題?

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ index.php?/$1 [L] 

    RewriteCond %{REQUEST_URI} ^application.* 
    RewriteRule ^(.*)$ index.php?/$1 [L] 

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


    RewriteCond $1 !^(index\.php|assets|myadmin|robots\.txt) 
    RewriteRule ^(.*)$ index.php?/$1 [L] 

</IfModule> 
<IfModule !mod_rewrite.c> 
    ErrorDocument 404 /index.php 
</IfModule> 

我可以確認assets/js是有效的文件夾。

回答

0

您是否可以通過瀏覽Web路徑到達您的資產文件夾?例如http://localhost/assets(嘗試檢索文件,如果您的網絡服務器未啓用索引,例如http://localhost/assets/css/style.css),它會加載嗎?

如果這是加載,您可能會調用您的資產,而不使用基本路徑,這意味着視圖試圖直接從它自己的文件夾中調用。

使用site_url()是一個非常好的步驟(url helper)...我強烈建議您通過製作一個簡單的幫助程序並將其添加到包含(如下所示)的自動加載中來簡化您的生活:

if (!function_exists('assets')) 
{ 
/** 
* Base asset directory call 
*/ 
function assets($var) 
{ 
    $CI =& get_instance(); 
    $CI->load->helper('url'); 

    return site_url('assets') . '/' . $var; 
} 
} 

然後,您可以把它在你的觀點:

<!-- load CSS --> 
<link href="<?php echo assets("css/bootstrap.min.css") ?>" rel="stylesheet"> 

然後,它會顯示這個一度呈現:

<link href="http://localhost/assets/css/bootstrap.min.css" rel="stylesheet"> 

這只是一個非常基本的例子,如果你有大量的資源需要調用頁眉或頁腳,我會建議創建一個幫助程序來基於配置文件加載,還可以指定在某些頁面加載時需要什麼這種方式以及它的動態加載與foreach。

也千萬小心調用資產(大寫字母)與資產(沒有大寫字母)有很大的不同,很多Web服務器是區分大小寫的...

我跑我的htaccess文件如下:

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule> 
+0

嗨,感謝您的回答。是的,我可以直接訪問資產文件夾(禁用默認目錄索引後)。是的,我在沒有基礎url的視圖文件中引用了/assets/css/styles.css或/assets/js/filename.js等。但是,我不認爲這是主要問題?我試圖把base_url這些引用,但它似乎沒有幫助? – NoNice

+0

當我查看我的網頁的源時,所有對/ assets/js的引用現在都帶有基本url,並且我仍然在我的日誌文件中找到兩個錯誤日誌條目以查找Assets/js未找到? – NoNice

+0

我添加了我的htaccess文件,沒有任何問題,我認爲可能是「RewriteBase /」也可能導致問題,請考慮將其作爲第一步刪除 –

相關問題