Search code examples
php.htaccesscodeigniterbase-url

CodeIgniter .htaccess and base_url() issues


I started working on CodeIgniter recently and I'm having some issues with the .htaccess and the base_url() function causing issues. I hope this is just a noob error and has a quick fix!

Pretty much what happens is that now with the .htaccess the index.php is gone from the url which is amazing but now I am having issues linking stylesheets and js files with my templates.

In all the tutorials I have read/watched, they have included the .htaccess and then still set their base_url() and then using "link_tag(href);" to link their .css but now I am getting a double up on the url. For instance when I tried to add the stylesheet, the path that it looked for was:

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

So my first thought was just to remove my base_url() so that the .htaccess does everything but it doesn't work. With the .htaccess I can have a site without the index.php being there but without stylesheets or JavaScript files linked or I can have a site with some styling, but deal with the index.php. There must be a way that both will work simultaneously?

Also, I did a fix by instead of using the link_tag() I just echoed out a normal link tag which works perfectly in the beginning when its still:

http://www.mysite.com/

But if the login is incorrect and I redirect to load the login view again, the styling disappears because its now using the relative path from:

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

Thanks in advance! Any help would be appreciated.


Solution

  • Try

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

    So www.mysite.com/css/mycss.css is retrieved normally where as www.mysite.com/something is passed to codeigniter.

    To get the css file with base_url() you would do base_url("css/mycss.css"), which should result in "http://www.mysite.com/css/mycss.css"