How do you combine multiple RewriteCond's and multiple RewriteRule's?

I’d like to have a set of rewrite conditions (boolean or), associated with a set of rewrite rules - but then followed by another set of rewrite rules that are not associated with the conditions. For instance

RewriteCond %{HTTP_HOST} ^firstdomain\\.com [OR]
RewriteCond %{HTTP_HOST} ^seconddomain\\.com [OR]
RewriteCond %{HTTP_HOST} ^thirddomain\\.com
RewriteRule ^images/(.*)$ images/$1 [QSA,L]
RewriteRule ^dir1/(.*)$ index.php?dir=dir1/$1.php [QSA,L]
RewriteRule ^(.*)/(.*)-search-(.*).html index.php?do=search&var1=$1&var2=$2&var3=$3 [QSA,L]

# Followed by rule that should not associated with the earlier RewriteCond's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Would the above accomplish this, or is there some mistake present?

i don’t follow what the RewriteCond at the top is suppose to be about…Do you need to list every domain that can be used to access your site? Could you not just redirect all to a single one? (Redirecting to one would also avoid duplicate content SEs might see.)

Nor do I understand the point of “^images/(.*)$ images/$1” that rule doesn’t appear to be needed.

l_e,

One thing at a time!

Kad,

mod_rewrite is NOT a scripting language so it does not have convenient “group” notation to have RewriteCond statements applied to multiple RewriteRules. On the other hand, there is the Skip flag which tells mod_rewrite to SKIP (read GOTO) the next n RewriteRules (block statements).

NOW, I must agree with l_e that your mod_rewrite code could use some improvement:

RewriteCond %{HTTP_HOST} ^firstdomain\\.com [OR]
RewriteCond %{HTTP_HOST} ^seconddomain\\.com [OR]
RewriteCond %{HTTP_HOST} ^thirddomain\\.com

can be written as a single statement:

RewriteCond %{HTTP_HOST} ^(first|second|third)domain\\.com$ [NC]

The atom allowed ONE of the entries inside to be matched; no www? accommodated; the No Case flag is NECESSARY because domains are NOT case sensitive.

RewriteRule ^images/(.*)$ images/$1 [QSA,L]

Well, l_e has a point as this is clearly “loopy.” REALLY! (.*) is the most ABUSED bit of regex code that there is and you’ve fallen into that trap. My advice is to LEARN SOME REGEX!

RewriteRule ^dir1/(.*)$ index.php?dir=dir1/$1.php [QSA,L]

Despite my comment about (.*) above, it appears to be a correct usage (assuming that you mean to capture NOTHING or ANYTHING - a BAD idea, nonetheless).

RewriteRule ^(.*)/(.*)-search-(.*).html index.php?do=search&var1=$1&var2=$2&var3=$3 [QSA,L]

OMG! Three of them! If index.php can handle nothing to absolute garbage, it may actually work - but that’s (IMHO) horrible coding.

# Followed by rule that should not associated with the earlier RewriteCond's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If all else fails, send ANYTHING (not null) to index.php? That makes index.php your 404 script.

It’s not my job to interpret your intentions but I’ve got to agree with l_e’s inference that you need to brush up on your regex before creating mod_rewrite statements. My tutorial article will help you with your regex but, unless I updated rather recently, I don’t have an example of the Skip flag ([QSA,L,S=3] will tell mod_rewrite to append any existing query string, that this terminates the mod_rewrite block statement and to SKIP the next 3 RewriteRules - the answer to your question).

Regards,

DK

Thanks guys. The first three rewrite rules above are actually just vague examples and not the specific ones I’ll be using.

The folder has multiple domains pointed to it actually. The CMS can serve each one differently.

What if one of them was .net? Say thirddomain.net intead of .com. I thought of the following

RewriteCond %{HTTP_HOST} ^(first|second|third)domain\\.(com|net)$ [NC]

But while that would work it wouldn’t be quite theoretically correct, since that would match extra domains.

Yes, it’s a modified Wordpress engine.

Where should I put the skip flag?

Kad,

The specific example I used (with the Last flag) would only (logically) be appended to a RewriteRule statement (and the Skip would only occur if that entire block statement were matched and the redirection performed).

FWIW, I still agree with l_e that your images/{GARBAGE} => images/$1 is “loopy” - that’s what the Pass Through directive is for (avoiding a loop).

Regards,

DK

Which rewrite rule would the skip flag be used on though?

The images rule was just some random thing and is not a rule I’m using :slight_smile: I just want something like:

#...RewriteCond's that specify a list of domains...
#...Some number of RewriteRule's to be used if one of the above RewriteCond's is matched...
# Followed by a rule that should not associated with the earlier RewriteCond's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Basically let’s say there are 10 domains pointed to this folder, and I want the first set of rewrite rules applied to them; and the final set of rules (the catch-all index.php rewrite) applied to all domains.

Kad,

You could start your domain list mod_rewrite with a RewriteRule that you want to emply on the non-listed domains then skip the ones that are not list block statements. Finish that with another which would skip all the would skip the others.

# RewriteCond %{HTTP_HOST} !(domain|list) [NC]
# RewriteRule {regex for NOT in domain list} {redirection} [L,S=10]
# 10 RewriteRule (mod_rewrite block statements) for non-list domains
# Last one should use a S=5 for 5 block statements for the listed domains

# 11th - start the listed domains block statements
# Any more than 5 would be processed normally (for listed and unlisted domains)

Regards,

DK