2012-06-25 80 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