Is there a way to redirect people from a page on my .com website to a corresponding page on my .co.uk website if they are in the UK
I’m hoping for a bit of help or to be pointed in the right direction!
I have three websites:
- a .co.uk website for the UK
- a .ie website for Ireland
- a .com website
for everyone else
The .com was my main site, and people in Ireland and the UK are used to visiting it, but now I need them to go to their local site, where the content has been tailored for them.
I host on WP Engine and have the GeoTarget add-on. For the past year, I’ve been using the code below to redirect everyone, but it’s a bit blunt. It sends everyone to the home page of their local site, even if they were trying to access a specific page e.g. ‘services’.
function country_geo_redirect() {
if ( is_user_logged_in() ) {
return;
}
$country = getenv('HTTP_GEOIP_COUNTRY_CODE');
if ( $country == "GB" ) {
wp_redirect( 'https://www.uk-website.co.uk', 301 );
exit;
} else if ( $country == "IE" ) {
wp_redirect( 'https://www.irish-website.ie', 301 );
exit;
}
}
add_action('init', 'country_geo_redirect');
It would be much better if I could edit this and redirect them to a page corresponding to the one they tried to visit e.g. if they tried to visit services, I should redirect them to the services page on the .ie or .co.uk
Any ideas?
Leave an answer