Forum Moderators: phranque

Message Too Old, No Replies

One condition - Multiple RewriteRules

MSN bot doesn't like 301 redirects - let's not give them to it.

         

tigertom

1:50 pm on Dec 22, 2006 (gmt 0)

10+ Year Member



I've been trying to set up multiple redirects from one
condition. This isn't working, and SE searches on this subject haven't helped:

## Redirect all save the MSN Bot
RewriteCond %{HTTP_USER_AGENT} !^(.*msnbot.*)$ [NC]
RewriteRule ^/zop [mysite.co.uk...] [R=301]
RewriteRule ^/free-doodads.shtml$ [mysite.com...] [R=301]
RewriteRule ^/free-doodads [mysite.com...] [R=301]
RewriteRule ^/jiminy/download [mysite.com...] [R=301]
RewriteRule ^/articles [mysite.com...] [R=301]
RewriteRule ^/filthy-lucre/contact.shtml$ [mysite.com...] [R=301,L]

jdMorgan

2:30 pm on Dec 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the docs:
The RewriteCond directive defines a rule condition. Precede a RewriteRule directive with one or more RewriteCond directives. The following rewriting rule is only used if its pattern matches the current state of the URI and if these additional conditions apply too.

Note the singular in all cases.

A RewriteCond affects the single RewriteRule which follows it. If you wish to affect more than one rule, you can either duplicate the RewriteCond(s) for each RewriteRule, or you can change the sense of the logic so that the following rule is NOT executed based on the RewriteCond, and then use that rule to skip the original rule(s), as so:


# Skip next six rules if user-agent starts with MSNbot
RewriteCond %{HTTP_USER_AGENT} ^msnbot [NC]
RewriteRule .* - [S=6]
#
# 301-Redirect all other user-agents
RewriteRule ^/zop/(.*)$ http://www.example.co.uk/zop/$1 [R=301,L]
RewriteRule ^/free-doodads\.shtml$ http://www.example.com/doodads/ [R=301,L]
RewriteRule ^/free-doodads/(.*)$ http://www.example.com/arooty/free-doodads/$1 [R=301,L]
RewriteRule ^/jiminy/download/(.*)$ http://www.example.com/download/$1 [R=301,L]
RewriteRule ^/articles/(.*)$ http://www.example.com/guff/$1 [R=301,L]
RewriteRule ^/filthy-lucre/contact\.shtml$ http://www.example.com/filthy-lucre/shylock-quote-form.shtml [R=301,L]
#
# Code execution for MSNbot resumes at this line
#

The above code is intended for use in httpd.conf, conf.d, or other server-config-level files. For use in .htaccess, it will be necessary to omit the leading slash on each RewriteRule pattern.

Note that the [L] flag is only applied if the rule matches. Therefore, it should be present on every line unless you have a good reason not to use it. Omit [L] only if subsequent rules must further rewrite the URL after the current rule has already rewritten it.

I made several other changes to the code to correct back-references to previously-unspecified subpatterns. These changes were based on an assumption of what you desired to do, as implied by the original code. Some or all may be wrong.

I disagree with your premise that MSNbot "doesn't like" 301 redirects, having successfully redirected MSNbot many times, but the above code snippet is nevertheless useful as a demonstration of using a negative-logic skip clause.

Jim

tigertom

9:57 pm on Dec 22, 2006 (gmt 0)

10+ Year Member



Jim, thanks very much. That's most informative.

I'm on a 14.4kbps connection here, so will give this code a try tomorrow.

It does sound odd that the MSN bot can't handle 301 redirects. Regardless, I shall eliminate one more possibility.

Perhaps people complaining of this are not writing the .htaccess code correctly (like me) i.e. it works in a browser, but bad formatting is causing the bot indigestion.

Or it could just be a bad supposition, full stop :)