plugin development - Append country to 'pretty' url but serve same page

For one particular page on my site I would like to append the country to the URL. The reasons for this are purely aesthe

For one particular page on my site I would like to append the country to the URL. The reasons for this are purely aesthetic so the customer knows that the prices on that page are in their own currency where their currency name/symbol is the same as others e.g. $ - USD, AUD, NZD etc

So the URL should be example/signup/nz or example/signup/au/. Each signup/<countrycode>/ then displays the same page (example/signup/) which handles the logic to display the correct prices/currencies for the users country. I'm not too worried if the home page also has /<countrycode>/ appended also but didn't want to append it across the entire site (at this stage).

I have a couple of questions related to this after looking at a number of articles on here.

  1. I believe I should be using add_rewrite_rule to achieve this - is this correct? I've tried a few combinations of this and navigating directly to /signup/nz/ (for example) just displays the 404 page (I am saving permalinks when I update the rewrite_rule). The rule I've mainly tried came from which I changed to the below: add_rewrite_rule('^signup/([^/]*)/','index.php?pagename=signup&country=$matches[1]','top'); and also tried add_rewrite_rule('^signup/([^/]*)/','index.php?pagename=signup','top');

  2. How do I get the site to automatically append this when the page itself is navigated to? e.g. if I'm on the home page and click the signup button do I need to on the home page geolocate the customer and have the href for the signup button updated for the customers location (e.g. to say the link is signup/nz/ or signup/gb/) or is this done somehow in the page?

From the reading that I've done if the above is the correct way to do this I believe there may be problems with duplicate content for SEO etc by doing this. I think I can get around this by adding the canonical name to the signup page. Is my thinking correct?

For one particular page on my site I would like to append the country to the URL. The reasons for this are purely aesthetic so the customer knows that the prices on that page are in their own currency where their currency name/symbol is the same as others e.g. $ - USD, AUD, NZD etc

So the URL should be example/signup/nz or example/signup/au/. Each signup/<countrycode>/ then displays the same page (example/signup/) which handles the logic to display the correct prices/currencies for the users country. I'm not too worried if the home page also has /<countrycode>/ appended also but didn't want to append it across the entire site (at this stage).

I have a couple of questions related to this after looking at a number of articles on here.

  1. I believe I should be using add_rewrite_rule to achieve this - is this correct? I've tried a few combinations of this and navigating directly to /signup/nz/ (for example) just displays the 404 page (I am saving permalinks when I update the rewrite_rule). The rule I've mainly tried came from https://wordpress.stackexchange/a/252843 which I changed to the below: add_rewrite_rule('^signup/([^/]*)/','index.php?pagename=signup&country=$matches[1]','top'); and also tried add_rewrite_rule('^signup/([^/]*)/','index.php?pagename=signup','top');

  2. How do I get the site to automatically append this when the page itself is navigated to? e.g. if I'm on the home page and click the signup button do I need to on the home page geolocate the customer and have the href for the signup button updated for the customers location (e.g. to say the link is signup/nz/ or signup/gb/) or is this done somehow in the page?

From the reading that I've done if the above is the correct way to do this I believe there may be problems with duplicate content for SEO etc by doing this. I think I can get around this by adding the canonical name to the signup page. Is my thinking correct?

Share Improve this question edited Jun 7, 2019 at 18:01 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Jun 7, 2019 at 16:08 JoshJosh 731 gold badge1 silver badge4 bronze badges 3
  • Why not just have one page with a query string? The user can still see that the currency or country matches what they're expecting, but then you won't have to mess with canonicals. – WebElaine Commented Jun 7, 2019 at 16:47
  • Could do. I wouldn’t know how to do that either to be honest. I’ll have a look tomorrow (any pointers on what to do for this) although if I’m going to do it I may as well make it nice. Are canonicals a pain? From what I read I thought I could just add it to the signup page with a reference to itself (may not have read what was required properly). – Josh Commented Jun 7, 2019 at 16:53
  • Canonicals aren't very tricky, but ensuring you just have one page to deal with (so you can also be certain you're just dealing with one template file that outputs everything) can make it simpler. In PHP you can access $_GET for anything in the query string, so if your URL is http://example/?country=india you would access $_GET['country'] to find out that "india" has been requested. – WebElaine Commented Jun 7, 2019 at 19:00
Add a comment  | 

1 Answer 1

Reset to default 1

If you are needing one rewrite rule for that single page, this snippet should get you there. You'll need to replace the XXXX in that snippet to be your post ID of your signup page.

function signup_pg_rewrite_rule() {
  add_rewrite_rule('^signup/([^/]*)/?','index.php?page_id=XXXX&country_code=$matches[1]','top');
}

add_action('init', 'signup_pg_rewrite_rule', 10, 0);

After you drop this in, you'll need to flush that rewrite rule array stored in the DB. The easiest way to do that is to login to WP, then Settings > Permalinks > And click the Save Changes button at the bottom. This will flush that array and rewrite for you. Any time you make changes to the rewrite rule array, you will always need to flush it.

Also as you eluded to, you'll need to setup a canonical tag on that particular page otherwise you are going to get flagged as duplicate content. If you are using Yoast, it has a Canonical field in it that would be easy to use. If you are wanting to always append a country code, you'll need to settle on one URL though. It can't be just /signup/ because that will always be 301 or 302 to '/signup/country/. (Assuming I understood you correctly)

As for automatically redirecting the page when a user shows up, you'll need to write some logic to check if the URL has been appended or not. If it hasn't, then you can move the user to the page you'd like them at. There are a few ways to do this, but maybe just using parse_url or $_SERVER would get you there. If you need something help with it, just drop a comment below.

if(url_has_not_been_appended){
  // Get the Country Code you want
  // Then get your current URL
  // Then let's redirect the user
  wp_redirect( $url.'country_code/', 301 );
  exit;
}

Hope that helps!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745426511a4627202.html

相关推荐

  • plugin development - Append country to &#39;pretty&#39; url but serve same page

    For one particular page on my site I would like to append the country to the URL. The reasons for this are purely aesthe

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信