homepage Welcome to WebmasterWorld Guest from 50.17.7.84
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
blank .htaccess
Quarfelburg




msg:1511664
 3:29 pm on Apr 10, 2003 (gmt 0)

Hi, I have a .htaccess that references an apache module that is a list of geographical ips. It automatically redirects the user to a .ca version of the site if the user is Canadian.

RewriteEngine on

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ [url.ca...] [L]

The problem is that url.ca is just a folder called "ca" in url.com, so I get a loop. If I redirect to any other external domain it works fine.

To solve this I placed a blank .htaccess in the "ca" folder. Apache didn't seem to recognise it so I placed an .htaccess with the following:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$

And it worked. I came in today, and it no longer works.

Any ideas?

 

jdMorgan




msg:1511665
 4:07 pm on Apr 10, 2003 (gmt 0)

Quarfelburg,

To prevent a loop, add code to stop the redirect to www.url.ca if you already are in www.url.ca:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteCond %{HTTP_HOST} !^www\.url\.ca$
RewriteRule .* http://www.url.ca/ [R=301,L]

As coded, this will redirect a request for any page in a non-.ca domain to the home page in .ca if the user's IP is determined to be Canadian. The important part here is that it redirects any page to the home page. Is that what you want to do?

Another note: In order to work, you must use the 301 external redirect as shown. If this is not acceptable (because you want to hide the redirect), you'll need to implement a cookie or a session-tracking variable to keep track of each visitor's current country-domain.

Jim

Quarfelburg




msg:1511666
 4:18 pm on Apr 10, 2003 (gmt 0)

Thanks Jim, I'll try it.

Why do I need to use the 301 redirect? Not trying to be difficult, just curious.

jdMorgan




msg:1511667
 4:22 pm on Apr 10, 2003 (gmt 0)

So that Apache "knows" that it's been redirected to the .ca domain, and the second RewriteCond can detect that fact.

Otherwise, that test would fail, and you're back to looping...

Jim

<added>Or is it not a different domain? - I may be misreading here.</added>

jdMorgan




msg:1511668
 4:51 pm on Apr 10, 2003 (gmt 0)

Quarfelburg,

I've got some other things I need to get done, and I'll be off-line for awhile, so here's a "just-in-case". Assuming that you meant to say www.url.com/ca, rather than www.url.ca, you can use this.

To prevent a loop, add code to stop the redirect to www.url.com/ca if you are already in www.url.com/ca:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteCond %{REQUEST_URI} !^/ca
RewriteRule ^(.*)$ /ca/$1 [L]

However, this redirects any page requested by a user with a Canadian IP to that same page in the /ca folder.

If you really want to redirect only to the /ca home page, use:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteCond %{REQUEST_URI} !^/ca
RewriteRule .* /ca/ [L]

This will mean that only the /ca homepage will be accessible to Canadian IPs.

HTH,
Jim

Quarfelburg




msg:1511669
 3:11 pm on Apr 14, 2003 (gmt 0)

Thanks JD! The following worked like a charm:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteCond %{HTTP_HOST}!^www\.url\.ca$
RewriteRule .* [url.ca...] [R=301,L]

Cheers,
Hayden

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved