Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

How to make single RewriteCond apply to multiple RewriteRules?

Is this possible?



6:56 pm on Jul 29, 2004 (gmt 0)

10+ Year Member

So my question is pretty simple -

I have a RewriteCond that controls whether I want a RewriteRule to take place - it makes sure image links and the css link don't break.

Problem is, I'm going to move towards using multiple rewrite rules for this particular rewrite, so that I can use a variable number of variables in the URI.

What I'm asking is, can I do something like

RewriteCond Cond [flag]

And have a single RewriteCond apply to all the rules? (braces shown just to indicate grouping, I'm aware there is no such syntax in htaccess :) )

Or am I left with no choice but to do this:

RewriteCond Cond [flag]
RewriteCond Cond [flag]
RewriteCond Cond [flag]
RewriteCond Cond [flag]

Any ideas? I've read the mod_rewrite documentation but it doesn't address this issue. I'm thinking I might be able to use RewriteMap in some way, but it doesn't address that issue at all and the documentation's description of RewriteMap is less than enlightening.

Quick note, I just commented out that rewrite Cond and it left my image and css links still working, which didn't work before so I think one of my later changes fixed that problem. So I don't need to immediately address this problem. The rest of my question remains though.

Here is what I was using:

# If the url isn't an image or the css file
RewriteCond %{REQUEST_URI}![a-zA-Z0-9]\.gif¦jpg¦css$

# Rewrite the url to the dynamic query string
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?/?([^/]+)? $1.php?id=$2&gal=$3&page=$4 [L]


7:24 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You can't make a single RewriteCond apply to multiple RewriteRules in a literal sense, but you can rearrange your logic using either of two methods:

Detect the false case of the original RewriteCond and terminate mod_rewrite processing:

RewriteRule \.(gif¦jpg¦css)$ - [L]
RewriteRule (whatever rules you want for non-image/css files go here)


Use the [S] (skip) flag to accomplish the same thing for that particular ruleset, but continue processing subsequent rules:

RewriteRule \.(gif¦jpg¦css)$ - [[b]S=3[/b]]
RewriteRule (rule #1 you want for non-image/css files goes here)
RewriteRule (rule #2 you want for non-image/css files goes here)
RewriteRule (rule [b]#3[/b] you want for non-image/css files goes here)
RewriteRule (this rule will be processed for all gif, jpg, and css files, or if all of the above 3 rules fail to match.)

Both of these methods require that care be taken in maintenance, so as to prevent later modifications from breaking the skip count (in example 2) or moving the bypass rule (first example) to a place that prevents it from working.

Very often, a problem that seems impossible to solve becomes possible by using negative logic -- by changing from "do something if condition" to "do something if not condition, or to "don't do something if condition."



7:29 pm on Jul 29, 2004 (gmt 0)

10+ Year Member


I think that will work! Thanks jd!


Featured Threads

Hot Threads This Week

Hot Threads This Month