Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.



Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.


Joomla URL redirection based on IP

Joomla URL redirection based on IP

Hello Stephen,

I have one Joomla installation that is accessible on 3 different front end URLs. For example...


How can I make either Meta-Mod Pro or Chameleon (whatever's best for the job (( I have both but am more versed in MetaMod)) ) redirect to the country specific URL based on user IP address?

For example, when a user from the UK enters domain.com in his browser, he gets automatically redirected to domain.co.uk - When someone in the states enters domain.co.uk he gets redirected to domain.com ...

In my case the traiiling parts of the URLs are identical so if a user from the UK enters domain.com/page1 it would be ideal if he gets redirected to domain.co.uk/page1 also.

I understand this will use the MaxMind database and also, the redirect should only occur once per say 30 mins or so allowing users to override the redirect in case of a bad IP address from MaxMind - So entering the first domain a second time the user from the UK could view the .com site.

Where to from here?


Administrator has disabled public posting

Re: Joomla URL redirection based on IP

Hi lowemail,

It sounds like you've got a good handle on the issues involved here. Since you have Chameleon I'd recommend sticking with that one as the rules trigger before the page is rendered, as opposed to MetaMod where the entire page gets constructed before the MetaMod might make a decision to redirect. That's relatively inefficient (slow).

Remembering redirects: this has a few challenges regarding setting a cookie on the domain you are redirecting AWAY from, but you can do this.

See http://stackoverflow.com/questions/6120 … ect-in-php for discussion of the issue.

So take this scenario:

1 - user is from UK
2 - user hits .com version of site
3 - .com site checks to see if there was a previous cookie set on .com. (If so, it had already redirected in the last 30 minutes.)
4 - there was no previous redirect, so...
--- .com sees user from UK
--- .com sets a cookie on the .com version, valid for 30 minutes.
--- then redirects user to .co.uk
5 - .co.uk does the same checks for cookie and GeoIP above, except for "visitor from the USA" so it can redirect back to .com if necessary
6 - in this case, since the user is from the UK there's no redirect necessary
7 - now the user decides they really do want the .com version, and type that into their browser, annoyed that they were redirected
8 - .com does the same checks as earlier (3-4), and decides not to redirect. SUCCESS.

However, if the user then tries the .net version, they will be immediately redirected to .co.uk since .net has no way of knowing that the user was already redirected from .com earlier.
Basically, the "memory" of being redirected lies in the particular domain they were redirected from, rather than the user carrying this info from domain to domain. See what I mean?

Second redirection issue you bring up: being able to redirect to the same relative URL on the "other" domain.

This is a good question and luckily Chameleon has a way of doing this. When you do a redirect you can put in tokens relating to the CURRENT URL. So you can redirect to http://domain.com/{path}{?query}{#fragment} which takes the current path, query string and fragment (if they were set) and subs in the new domain name.

Putting all these together:

I'm going to assume:
1. The .net version will redirect UK visitors to the .co.uk version and US visitors to the .com version, just like the other sites will.
2. None of the sites will redirect anyone to the .net version.
3. Visitors from countries other than US and UK won't be redirected at all. e.g people from FR are allowed on .co.uk or .com. [an alternative is to say ".co.uk is for UK ONLY" and ".com is for US ONLY" but we're not doing that in this example]

Ok let's think about the logic, which will then go into Chameleon rules:
1. detect cookie on current domain. If this is found, abort the rules immediately because we've already redirected in the last 30 minutes from this domain, and we're not going to do so again. (this is irrespective of what domain we are on)
2. detect GeoIP "GB" and domain ".com or .net". If found, set the 30minute cookie and do the redirect to the .co.uk site
3. detect GeoIP "US" and domain ".co.uk or .net". If found, set the 30minute cookie and do the redirect to the .com site

To set this up in Chameleon will require a couple of snippets of PHP code (in the PHP boxes) to handle the exact syntax for setting and detecting the 30 minute cookies. But that's pretty easy to do and I can clarify later if you require assistance. The rest of the detection of domains and the redirection itself I think can be done with the GUI controls.

Hope this helps,

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

3330 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
Total Topics:
Total Polls:
Total Posts:
Posts this week:
User Info:
Total Users:
Newest User:
Members Online:
Guests Online:

There are no members online

Forum Legend:

 New Sticky