Phill Pafford
Phill Pafford

Reputation: 85318

How do you combine these 2 .htaccess RewriteRules into one?

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?

Upvotes: 2

Views: 842

Answers (2)

djc
djc

Reputation: 11711

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).

Upvotes: 0

David Z
David Z

Reputation: 131600

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.

Upvotes: 4

Related Questions