Welcome Guest, Not a member yet? Register   Sign In
Linking to CSS With index.php Removed by .htaccess
#1

[eluser]Walter Coots[/eluser]
I just got started with CodeIgniter earlier this afternoon, and I'm really loving it. I did some poking around on the forums for best practices in terms of frontend asset organization, and personally I like having an assets directory either in /application/ (I moved it out of /system) or just within /. I've also used a .htaccess file to remove index.php from the url of the site, which I think is causing problems.

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

My trouble is that I'm having trouble figuring out how to reference my frontend assets. For all intents and purposes, this is a file:

http://sandbox.localhost/assets/css/default/screen.css

But when I go directly to it, I'm receiving a 404 error presumably generated by CI not finding a controller named "assets". (http://sandbox.localhost/index.php/asset...screen.css) This happens no matter where I try to place the files.

I looked into base_url() and site_url(), but I don't know if they'd resolve the problem. Plus there are instances where I load a CSS reset from within another CSS style sheet, or include a JavaScript file within another.

Any guidance would be much appreciated!
#2

[eluser]mddd[/eluser]
Look at you .htaccess and realize what it is doing. It is rewriting everything to index.php, except if the url already starts with index.php, images or robots.txt.
The solution is simple: add your exceptions to that rule. If you put everything in a folder called assets, change the condition to
Code:
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
By the way, I would add favicon too, because otherwise every request gives you a 404 error on favicon.ico. (If you have none, of course the error will remain).

Another solution is to add a condition so that there will be no rewrite if the requested url is an actual file:
Code:
RewriteCond %{REQUEST_FILENAME} !-f
But then you should check that all files are indeed safe to show to the user of course.
#3

[eluser]Walter Coots[/eluser]
Ahhh, thanks so much. I just copied the .htaccess contents from the wiki or user guide, not fully understanding how it worked. For example, I have no idea what $1, [L]. But it looks like simply replacing |images with |assets or adding |assets afterward remedies my problem. Nice.

Good call on the favicon, too! I'll definitely be using one.
#4

[eluser]phpgeek[/eluser]
This also helped me a lot, thank you very much indeed!




Theme © iAndrew 2016 - Forum software by © MyBB