Forum Moderators: phranque

Message Too Old, No Replies

linking rules together...

mod_rewrite

         

bull

3:45 pm on Aug 14, 2004 (gmt 0)

10+ Year Member



Dear mod_rewrite experts,
in order to sweep Yahoo!s index of old, no longer existent filenames I renamed long ago, I decided to "cloak" and presend dumb Slurp a 404, but redirect the rest of legitimate users to the new page. I did this by the following:

RewriteCond %{HTTP_USER_AGENT}!Slurp
RewriteRule (.*)oddkw1oddkw2_.+\.htm$ $1oddkw1-oddkw2-$2\.htm [R=301]
RewriteCond %{HTTP_USER_AGENT}!Slurp
RewriteRule (.*)foo1_foo4_.+\.htm$ $1foo1-foo4-$2\.htm [R=301]
...

and 50 more in this style. This works of course very well. My question: Is there any way to "link" the RewriteRules together, so that I need only one RewriteCond, but all Rules only work under that condition? All I tried did not work. I mean: if not Slurp then redirect oldfile1 to newfile1 and oldfile2 to newfile2 and ...

Your help is much appreciated. Perhaps, or most likely, the question is dumb.

jdMorgan

6:32 pm on Aug 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bull,

See the [S] flag to skip over a block of RewriteRules. You could use it like this:


# Skip next 20 rules if Slurp
RewriteCond %{HTTP_USER_AGENT} Slurp
RewriteRule .* - [S=20]
# Do rewrites for all but Slurp
RewriteRule <first rule for non-Slurp UA's
RewriteRule <18 more rules for non-Slurp UA's
RewriteRule <last rule for non-Slurp UA's
# Slurp skips the 20 rules above, and mod_rewrite parsing resumes here
#
# Skip next 32 rules if not Slurp
RewriteCond %{HTTP_USER_AGENT}!Slurp
RewriteRule .* - [S=32]
# Do rewrites for Slurp
RewriteRule <first rule for Slurp
RewriteRule <30 more rules Slurp
RewriteRule <last rule for Slurp
# Non-slurp UA's skip the 30 rules above, and mod_rewrite parsing resumes here.
#
<Remaining rules for non-redirected URLs.>

This works well, but be very, very careful to count your rules accurately!

(If you want to get tricky, you can actually skip 21 rules in the first section, since you don't need to re-test the User-agent in the second section once you know it's Slurp. If this isn't clear, then don't worry about it.)

Jim

bull

8:46 pm on Aug 14, 2004 (gmt 0)

10+ Year Member



Works. Simplay great! Thank you very much, Jim!