2012-06-25 79 views
0

我最近開始使用CodeIgniter,並且遇到了一些與.htaccess和base_url()函數有關的問題。我希望這只是一個noob錯誤,並有一個快速修復!CodeIgniter .htaccess和base_url()問題

現在發生的事情是,現在使用.htaccess index.php從url中消失了,這是驚人的,但現在我遇到了將樣式表和js文件與我的模板鏈接的問題。

在所有我已閱讀/觀看的教程中,他們都包含了.htaccess,然後仍然設置了其base_url(),然後使用「link_tag(href);」鏈接他們的.css,但現在我得到了一個雙倍的網址。例如,當我試圖添加的樣式表,它尋找的路徑是:

http://www.mysite.com/site/www.mysite.com/site/css/stylesheet.css 

所以,我首先想到的是剛剛刪除我BASE_URL(),從而使的.htaccess做的一切,但它不工作。使用.htaccess我可以有一個沒有index.php的站點,但沒有鏈接樣式表或JavaScript文件,或者我可以使用一些樣式的網站,但要處理index.php。必須有一種方法可以同時工作?

而且,我通過而不是使用link_tag()做了修復我只是呼應了正常的鏈接標籤,它在一開始完美的作品時,其仍:

http://www.mysite.com/ 

但如果登錄不正確,我重定向到再次加載登錄視圖,造型消失,因爲它現在使用從相對路徑:

http://www.mysite.com/user/login 

提前感謝!任何幫助,將不勝感激。

回答

1

嘗試

RewriteEngine on 
RewriteCond $1 !^(css|js) 
RewriteRule ^(.*)$ /index.php/$1 [L] 

那麼,作爲www.mysite.com/something傳遞給笨www.mysite.com/css/mycss.css正常檢索。

要獲得base_url() CSS文件,你會做base_url("css/mycss.css"),這將導致"http://www.mysite.com/css/mycss.css"

+0

謝謝!這工作! – nickcorin

+0

@nickcorin如果這對你有用,請考慮upvoting並接受答案。謝謝。 – madeye

1

一個更好的.htaccess規則集將是:

#Removes access to the system folder by users. 
#Additionally this will allow you to create a System.php controller, 
#previously this would not have been possible. 
#'system' can be replaced if you have renamed your system folder. 
RewriteCond %{REQUEST_URI} ^system.* 
RewriteRule ^(.*)$ /index.php?/$1 [L] 

#When your application folder isn't in the system folder 
#This snippet prevents user access to the application folder 
#Submitted by: Fabdrol 
#Rename 'application' to your applications folder name. 
RewriteCond %{REQUEST_URI} ^application.* 
RewriteRule ^(.*)$ /index.php?/$1 [L] 

#Checks to see if the user is attempting to access a valid file, 
#such as an image or css document, if this isn't true it sends the 
#request to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L] 

這不允許到您的系統和應用程序文件夾的訪問,但允許訪問所有其他文件,例如:/ js/*,/ css/*,/ img/*等。

+0

你可以簡單地把PHP文件放在其他地方,只需要在這個目錄下index.php(和靜態目錄如css/js) – Esailija