views:

1017

answers:

3

I have been using simple mod_rewrite rules for my CMS for years and am now making a new version, I am seeing the rewriteCond not making sense- I have the standard "if is not a file" but I still see the rewriterules being evaluated even though they're not supposed to. My rewrite code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&var=$2

I load /page/var and it works fine as index.php?page=page&var=var, but I try to load /css/file.css and it loads index.php?page=css&var=file.css even though /css/file.css is a file, so the whole rewrite section shouldn't even be evaluated.

I've never seen htaccess apparently defy its own logic, can someone help me figure this out? Has anyone ever run across anything like this?

A: 

Well, that should work.

Try setting the following props in your .htaccess:

RewriteLog /var/log/rewrite.log
RewriteLogLevel 3

To debug your requests. Remember to reset this value once you're done, otherwise you'll end up with a filled up harddrive.

alexn
+3  A: 

The RewriteConditions only apply to the next rule. You want this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&var=$2
Greg
That's great, after all these years doing mod_rewrites like this it turns out my logic worked by accident... thanks so much
A: 

Perhaps using RewriteLog and RewriteLogLevel to debug what it's doing?

(From http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)

Douglas Leeder