views:

159

answers:

3

Hi,

I have the following rewrites in my .htaccess:

Options +FollowSymLinks 
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteRule ^index/error/([^/\.]+)/?$ index.php?error=$1 [L]

As you can tell, it's supposed to not rewrite any .css/.jpg/.jpeg/.gif/.png files. Despite that, it's doing so. What's really odd is that the line below it, the one that redirects /index/error/whatever to index.php?error=$1, is working perfectly. Is there something wrong with my regular expression? Is there a better way to do this?

TIA.

EDIT after many attempts at fixing the problem later:

I actually found a different, (in my opinion) more elegant, certainly simpler way to do this. My problem was that my file contained a pointer to my CSS file as href="./css/styles.css". The styles.css file is never, ever going to move. So, I just changed the href element to be an absolute URI (http://www.mysite.com/css/styles.css) instead of a relative URI. Problem solved! Thanks to everyone who posted for their help.

+2  A: 

You can use a RewriteCond to avoid applying rules to that set of extensions, like

Options +FollowSymLinks 
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css|jpe?g|gif|png)$
RewriteRule ^index/error/([^/\.]+)/?$ index.php?error=$1 [L]

Also, you don't mention what requests are failing and in what way, that would be nice to know if this isn't enough to solve your problem (which is likely).

Vinko Vrsalovic
+1 because I had this exactly problem (not using RewriteCond) my first time playing with URL rewriting.
musicfreak
Unfortunately, no, you're right, that doesn't seem to solve it. The specific problem I'm having is that when requesting my CSS and image files, Safari (and Firefox, and Stainless) is looking in /index/error/-1/ (for example) as a directory, rather than following the rewrite rule to stay put in the root directory. Does that explain it enough?
benjy
+1  A: 

The 'last' rule is not very useful in .htaccess when the url is modified - see mod_rewrite tech. In there, you'll see this is because the rewrite occurs late in the request processing so it knows the directory but then it's too late to rewrite the url. It solves this problem by creating a new request and that new request gets processed again by the same rules effectively negating the "last" flag.

I can't see how this applies in your case. The '-' shouldn't be a url rewrite. From this web site they recommend using the commands below to debug rewrite and you should see why your rule appears to be ignored.

    You can debug mod_rewriting adding those two directives in httpd.conf:
    RewriteLogLevel 9
    RewriteLog "<path-to-log-file"

The regex looks good to me and I saw nearly the same one in a mod_rewrite example.

Tony Lee
A: 

I actually found a different, (in my opinion) more elegant, certainly simpler way to do this. My problem was that my file contained a pointer to my CSS file as href="./css/styles.css". The styles.css file is never, ever going to move. So, I just changed the href element to be an absolute URI (http://www.mysite.com/css/styles.css) instead of a relative URI. Problem solved! Thanks to everyone who posted for their help.

benjy
This makes me wonder if the '-' does really look like a rewrite as it goes from relative to an absolute uri, even though there is no substitution.
Tony Lee
Me too. I wish I knew enough to troubleshoot better!
benjy