Mod Rewrite breaks CSS

I just added a mod rewrite to eliminate the index.php from my URL. After doing so, my page loads except for the CSS. I can’t even directly access the CSS directory (http://mysite.com/css/style.css). Without the rules in .htaccess if I went to that link I would see the plain text of my CSS file in the browser. With the rules I have listed below in .htaccess I can’t access the css file (get a 404 error).

I have read all kinds of solutions saying to use absolute paths, but that won’t work (as far as I can tell ) in my instance as the directory returns a 404, it is not an issue of not directing to the proper directory.

Here are the contents of my .htaccess:

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

This is my first mod rewrite and I am still getting my head around what all this means. Hopefully someone has a solution as I would prefer to NOT advertise that I am using php to minimize hack attempts (how ever minutely it will minimize it is still something).

Greg

As usual, I get frustrated, post a request for assistance, and then the solution slaps me in the face.

I added CSS to the following line:
RewriteCond $1 !^(index\.php|images|robots\.txt)

To make it:
RewriteCond $1 !^(index\.php|css|images|robots\.txt)

It appears that tells it to ignore those directories? I was a little stumped because my images were showing, that is what made this solution pop out at me. Guess I need to read up on rewrite rules and how they are structured.

Greg

For what it’s worth, this problem is more commonly solved like this:

RewriteCond %{REQUEST_FILENAME} !-f

This says, apply the rewrite only if the request is not for a real file. Real files include the index.php file, the robots.txt file, all your image files, all your CSS files, and any other real file you might not have thought of (such as JS files or favicon.ico).

Greg,

That’s the problem with (.*):

[rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/rant #1]

Okay, !-f should take care of your problems although I prefer to use my mod_rewrite to screen requests (you had EVERYTHING/NOTHING redirected to index.php). Don’t you just want all .php scripts other than index being redirected (leaving images, css and js alone)?

Put yourself in the position of mod_rewrite and think about your redirectionS - mod_rewrite is restarted every time a match/redirection is successful so “get loopy” with mod_rewrite and you’ll see the effect of bad code.

Regards,

DK

It’s worth noting that the use of (.) is both standard practice and frequently used even in the official documentation itself. There’s nothing wrong or lazy about it. This case in particular seems to be a perfectly good use for it. If the goal is to match any request that isn’t a real file, then (.) will match any request, exactly like we want, and the condition will exclude real files, exactly like we want.