admin管理员组

文章数量:1122832

I have a WP question hoping someone can point me in right direction.

I need to redirect my store customers to a specific country subdomain for all pages, posts, and products. The main domain and subdomain have the same content, so I only need to change the URL.

For Example: www.example main page www.example/page1 , www.example/product1

For US visitor: us.example main page us.example/page1 , us.example/product1

For UK visitor: uk.example main page uk.example/page1 , uk.example/product1

Thank you.

I have a WP question hoping someone can point me in right direction.

I need to redirect my store customers to a specific country subdomain for all pages, posts, and products. The main domain and subdomain have the same content, so I only need to change the URL.

For Example: www.example.com main page www.example.com/page1 , www.example.com/product1

For US visitor: us.example.com main page us.example.com/page1 , us.example.com/product1

For UK visitor: uk.example.com main page uk.example.com/page1 , uk.example.com/product1

Thank you.

Share Improve this question edited Jul 24, 2024 at 20:04 Tony Djukic 2,2594 gold badges18 silver badges33 bronze badges asked Jul 24, 2024 at 1:29 ParadivaParadiva 1 1
  • you can do this with the help of the polylang plugin wordpress.org/plugins/polylang – mmm Commented Jul 24, 2024 at 2:57
Add a comment  | 

1 Answer 1

Reset to default 0

You can try to check the visitor country via the geojs.io, and then build the logic for redirecting.

See the JS code below:

geoTargeting();

  function geoTargeting() {
    return new Promise((resolve, reject) => {
      fetch('https://get.geojs.io/v1/ip/country.json').then(response => response.json()).then(data => {
        const currentHostname = window.location.hostname;
        const currentPath = window.location.pathname;
        let newUrl;
        if (data.country === 'US') { // Check if country US
          const subdomain = 'us';
          newUrl = window.location.protocol + '//' + subdomain + '.' + currentHostname + currentPath;
          window.open(newUrl); // Redirect
        } else {
          resolve(false);
        }

      }).catch(error => {
        console.error('Error:', error);
        resolve(false);
      });

    });
  }

本文标签: url rewritingRedirect all URLs in WordPress and Permalink to by country to subdomain