views:

259

answers:

2

Ok I have another question and I'm a beginner at this.

I have this RewriteRule, it redirects the query correctly but doesn't allow me to use the other directories:

RewriteRule ^([0-9A-Za-z]+)/?$ /query.php?id=$1 [L]

and now this RewriteRule to skip all these directories but now the rule above needs to be commented out for this to work.

RewriteRule ^(css|js|admin|pages|includes|images)(/|$) - [L]

Can I combine the two? If so, how?

+3  A: 

RewriteRules are checked in the order they occur in the file, so if you put the css|js|admin|pages|includes|images rule first, it will match first and stop the rewriting process before the other rule is reached. Just make sure to keep the [L] flag at the end of that rule.

David Zaslavsky
Thanks just needed to confirm what I was seeing was correct
Phill Pafford
A: 

There's also this neat trick:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) query.php?id=$1 [L]

That is, if the file path is not an existent file or directory, send the request to a PHP script (so that you may load some module dynamically or show a useful 404 page).

djc
thanks, so this is like a 404 redirect? where do I place the custom 404 page?
Phill Pafford
It redirects everything (that's not a file or dir in your filesystem) to the PHP script, meaning you can deal with it there. If, by some internal routine, you determine that the requested URI should not exist, you can send a 404 status error and error page contents.
djc