Forum Moderators: phranque

Message Too Old, No Replies

Geotargeting via htaccess on a CDN

         

Sgt_Kickaxe

9:11 am on Mar 28, 2011 (gmt 0)



.htaccess is not my strong suit and I've found bits and pieces of what I need to know in various posts but not the whole shebang, any guidance welcome.

What I have:
- A website for U.S. visitors with a subdomain for Canadian visitors.
- Both are on a CDN which obfuscates the ip occasionally, it's a proxy thing, causing my non htaccess based geotargeting to fail.
- The CDN does provide a country code in referrer data, $_SERVER['HTTP_CF_IPCOUNTRY'] AA, with AA representing the visitors country. Canada is CA, U.S. is US, of course.

What I need:
- If a visitor to the US site is CA based he is redirected to the CA subdomain - [ca.example.com...]
- If a visitor the the US site is based in the US or in another country besides CA nothing happens, they stay on http://www.example.com.
- the redirect to send the visitor to the SAME url, only changing the www to ca and vice versa, the sites share the same url structure.

What I know:
- I need to add the code the .htaccess files in both the main site root and the subdomain root.
- I need to set the condition to check the country code and redirect if necessary.
- I don't want to block anyone, just to check if a redirect is in order.
- I need the code not to cause problems if a file is served by my server instead of the CDN, in which case there will be no $_SERVER['HTTP_CF_IPCOUNTRY'] set.


I'd post my best effort, but there have been two dozen or so attempts that don't work fully and I'm a little lost at this point... advice?

Sgt_Kickaxe

10:27 am on Mar 28, 2011 (gmt 0)



RewriteEngine on
RewriteCond %{ENV:CF-IPCountry} CA
RewriteCond %{HTTP_HOST}!^ca\.example\.com$
RewriteRule ^/(.+) [ca.example.com...] [R=301,L]

That was my last attempt, internal server error is the result. I'm not sure how to properly set the condition to check the CF-IPCountry code.

Sgt_Kickaxe

4:50 pm on Mar 28, 2011 (gmt 0)



Still working on this. The following, when placed on the US site htaccess and visited from CA doesn't throw an error but doesn't redirect.

SetEnvIf CF-IPCountry CA RedirectVisitor=yes

RewriteCond %{Env:RedirectVisitor} yes
RewriteCond %{HTTP_HOST} !^ca\.example\.com$
RewriteRule ^example.com/(.+) [ca.example.com...] [R=301,L]


I'm just not seeing what I'm missing.

Sgt_Kickaxe

6:12 pm on Mar 28, 2011 (gmt 0)



Over 8 hours spent on this thus far and some magic, not sure if it's robust or not but step one - got it working with...

SetEnvIf CF-IPCountry CA RedirectVisitor=yes

RewriteCond %{ENV:RedirectVisitor} yes
RewriteRule ^(.*)$ [ca.example.com...] [R=301,L]

I have a sneaking suspision I'm about to run into troubles however. A condition to make sure images aren't redirected for starters.

Sgt_Kickaxe

7:59 pm on Mar 28, 2011 (gmt 0)



Yup, I need images, .css and urls ending in "_US" not to be redirected. I'm also really not sure about the "^(.*)$" in the rewriterule.

If anyone has any suggestions I'm all ears.