Web Hosting Talk







View Full Version : htaccess redirect


larwilliams
01-25-2010, 12:58 AM
Hi guys,

I am looking to redirect cart.php to another page on my site if the request comes from a certain country. I've got it partially figured out (using GeoIP and Maxmind). But cannot get the .htaccess rules quite right to handle it

So far, I got this:


# Redirect Vietnam country to rejected page
GeoIPEnable On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^VN$


I need the RewriteCond to check if the page being requested is cart.php and the Rewrite rule to redirect to another page on the main folder of our site (example: /banned.php )

Hope someone can help!

mattle
01-25-2010, 01:05 PM
Okay, so you read Apache's docs, and tried what exactly to write the other condition and the rule? It's hard to troubleshoot non-existent syntax!

larwilliams
01-25-2010, 01:14 PM
Okay, so you read Apache's docs, and tried what exactly to write the other condition and the rule? It's hard to troubleshoot non-existent syntax!

Here is the rule exactly as tried:


# Redirect Vietnam country to rejected page
GeoIPEnable On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^VN$
RewriteCond %{REQUEST_URI} ^cart.php [NC]
RewriteRule ^(.*)$ http://www.lcwsoft.com/page.php [L]

mattle
01-25-2010, 01:25 PM
Here is the rule exactly as tried:


# Redirect Vietnam country to rejected page
GeoIPEnable On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^VN$


The first thing I'd do is comment this out, until you're sure the following condition and rule are working as expected--eliminate your potential source of problems.



RewriteCond %{REQUEST_URI} ^cart.php [NC]

According the the apache docs (http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteCond -- look under the notice in the Server Variables section) this mirrors the HTTP GET request, and thus should begin with a forward slash:


RewriteCond %{REQUEST_URI} ^/cart.php [NC]

RewriteRule ^(.*)$ http://www.lcwsoft.com/page.php [L]


The parentheses here are extraneous. You're not doing anything with $1...

Also, I don't know if you left it out for brevity, but before any of this, you should have:

RewriteEngine On

larwilliams
01-25-2010, 01:47 PM
The first thing I'd do is comment this out, until you're sure the following condition and rule are working as expected--eliminate your potential source of problems.



According the the apache docs (http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteCond -- look under the notice in the Server Variables section) this mirrors the HTTP GET request, and thus should begin with a forward slash:


RewriteCond %{REQUEST_URI} ^/cart.php [NC]

The parentheses here are extraneous. You're not doing anything with $1...

Also, I don't know if you left it out for brevity, but before any of this, you should have:

RewriteEngine On

Hi,

I did leave out the RewriteEngine On bit, as it is declared earlier in the same .htaccess file for an unrelated rule

I've tested the GeoIP bit using examples from the Maxmind web site and it works as expected, so I don't think the error is there.

I will try your suggestions and post back shortly.

larwilliams
01-25-2010, 02:20 PM
This works


# Redirect Vietnam country to rejected page
GeoIPEnable On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^cart.php$ http://www.lcwsoft.com/banned_countries.php [R=301,NC,L]

mattle
01-25-2010, 03:31 PM
Right...in the context of RewriteRule, you don't precede your URI with a forward slash...

larwilliams
01-25-2010, 03:56 PM
Right...in the context of RewriteRule, you don't precede your URI with a forward slash...

Okay, but I am assume this rule is then applied to every incoming request for the folder the .htaccess file is in.

Assuming the request is for a cart.php file, how do I write a RewriteCond to work in this situation?

mattle
01-25-2010, 10:02 PM
I don't know that there's much of a difference between putting the bit that matches "cart.php" in the search pattern for the RewriteRule, or by writing a RewriteCond that checks for it in the REQUEST_URI.

Either way if you're checking it against %{REQUEST_URI}, I would still preface the search pattern with a forward slash, as I suggested in post #4...have you tried that?