Multiple RewriteConds for Multiple RewriteRules

Can someone please show me what I doing wrong. I want to be able to redirect traffic from a specific refferer url to a specific site and redirect traffic from another specific referrer to another specific site. The problem I am running into is that traffic from both referrers are going to the url defined in the first RewriteRule. The second RewriteRule is not working . Here is an example of what I have done:

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} htaccesstest\.blogspot\.com\/2010\/05\/first-post-and-test\.html [NC,OR]
RewriteRule ^/?$ “http\:\/\/yahoo\.com” [R=301,L]

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} www\.squidoo\.com\/testing-htaccess-scripts [NC,OR]
RewriteRule ^/?$ “http\:\/\/google\.com” [R=301,L]

Thanks,
Mike

David,

Thank you for the warm welcome and the tips. I cleaned up the code a bit. Here’s what I have now:

RewriteEngine on
# Options +FollowSymLinks
RewriteCond %{HTTP_REFERER} ^http://htaccesstest\\.blogspot\\.com/2010/05/first-post-and-test.html$ [NC]
RewriteRule .* http://www.yahoo.com [R=301,L]

RewriteCond %{HTTP_REFERER} ^http://www\\.squidoo\\.com/testing-htaccess-scripts$ [NC]
RewriteRule .* http://www.google.com [R=301,L]

I’ve created a Squidoo Lens and a Blog with links to a subdomain I created to test the redirection. At this point, only one RewriteRule is working and both referer sites are being redirected to Google.com when I test the links.

From what I’ve researched, each RewriteRule is associated with the preceeding RewriteCond, but the code doesn’t seem to be executing properly.

Mike,

First, WELCOME to SitePoint’s Apache forum!

Second, you’re at the unusual extreme of wanting to escape characters that DON’T need to be escaped (/'s). Second, the OR flag means that the RewriteCond need NOT be matched.

Third, WHY are you repeating RewriteEngine on followed by Options +FollowSymlinks. That makes you look like a … well, let’s not go there. The +FollowSymlinks should be in the httpd.conf so you don’t need that; the RewriteEngine on is proper technique but is ONLY used to ensure that mod_rewrite is not in the comment mode, i.e., use it like /* … */ in PHP where the on value is the */ (end of comment block).

Regards,

DK

mm,

You should remove the commented line as you’ve proven to yourself that it’s not required.

Is your {HTTP_REFERER} correct? Both of them?

I consider it poor coding to do anything more than necessary in the regex so I’d also use .? rather than .* in the RewriteRules. VERY minor point, though.

These redirections should be working fine - unless there’s something else going on (cPanel redirections?). Also, you mentioned that you’ve setup those pages to refer FROM. Where are they referring to, where the .htaccess is located?

As for “associations” between RewriteRules and RewriteCond statements, you’ve misunderstood. What you should concentrate on is the mod_rewrite BLOCK statement, i.e., every mod_rewrite directive that’s “terminated” by a Last flag. That means that you can have a series of RewriteCond AND RewriteRule statements which, unless the OR flag is used, they will ALL be ANDed with the terminating RewriteRule’s Last flag. Think of it in terms of an invisible { at the beginning of a block of statements and terminated by a Last flag rather than a closing } - the only difference is the IMPLIED AND between each statement. This is IMPORTANT to understand so ask if you didn’t comprehend fully.

Regards,

DK