homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How to make single RewriteCond apply to multiple RewriteRules?
Is this possible?
TheBlueEyz




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

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]
{
RewriteRule1
RewriteRule2
RewriteRule3
....
}

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]
RewriteRule1
RewriteCond Cond [flag]
RewriteRule2
RewriteCond Cond [flag]
RewriteRule3
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.

Added:
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]


 

jdMorgan




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

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)

-or-

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

Jim

TheBlueEyz




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

Hmmmm....

I think that will work! Thanks jd!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved