Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to use more than one RewriteCond for a redirection with this rule: if it has 1 or all of these specified, do not redirect it.

.htaccess
RewriteCond %{HTTP_USER_AGENT} !Googlebot
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !fr
RewriteCond "%{HTTP_REFERER}" "!test.com"


What I have tried:

It seems I need to use some flags in the end of rewritecond.
Posted
Updated 23-Aug-23 4:09am
v3

1 solution

Use the '[NC]' flag to make the pattern matches case-insensitive. Use the '^' and '$' symbols in the country code and 'referer' conditions indicate the start and end of your respective strings - RewriteRule Flags[^]

The RewriteRule '^ - [L]' line used in my code is used to indicate that if any of the specified conditions are met, no further rewrite rules should be processed and the request should not be redirected, the '-' means no redirection -
Apache
RewriteEngine On

#Check if the user agent is not Googlebot...
RewriteCond %{HTTP_USER_AGENT} !Googlebot [NC]

#Check if the country code is not 'fr'...
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^fr$ [NC]

#Check if the HTTP referer is not 'test.com'...
RewriteCond %{HTTP_REFERER} !^https?://test\.com [NC]

#Check if the requested URL is 'page.php'...
RewriteCond %{REQUEST_URI} ^/page\.php/?$ [NC]

#Combine the conditions using the [OR] flag...
RewriteCond %{HTTP_USER_AGENT} !Googlebot [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^fr$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://test\.com [NC]
RewriteCond %{REQUEST_URI} ^/page\.php/?$ [NC]
RewriteRule ^ - [L]

#Your existing redirect code...
RewriteRule ^page.php/?$ $1/page2.php$2 [R=301,L]
 
Share this answer
 
v2
Comments
Userxman0 23-Aug-23 7:20am    
right now im using this redirect code RewriteRule ^page.php/?$ $1/page2.php$2 [R=301,L]

so , how should be use it with your rewritecond?
Andre Oosthuizen 23-Aug-23 7:28am    
You should have mentioned this in your question, would have saved me some time... I have updated my solution. I suggest you read the link provided to understand what the conditions are for redirecting.
Userxman0 23-Aug-23 12:15pm    
are you sure this referer code is working ? or its only working on home page of test.com and not working on sub pages? like ; test.com/page1.html
Andre Oosthuizen 24-Aug-23 5:04am    
Have you set up the appropriate environment variable 'GEOIP_COUNTRY_CODE'. Also, ensure that you've enabled the necessary modules like 'mod_rewrite' and 'mod_geoip' for these conditions to work properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900