Welcome to WebmasterWorld Guest from 54.196.232.162

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

blank .htaccess

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

Junior Member

10+ Year Member

joined:Feb 4, 2003
posts:61
votes: 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?

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 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

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

Junior Member

10+ Year Member

joined:Feb 4, 2003
posts:61
votes: 0


Thanks Jim, I'll try it.

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

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 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>

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 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

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

Junior Member

10+ Year Member

joined:Feb 4, 2003
posts:61
votes: 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

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members