1

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.com/signup/nz or example.com/signup/au/. Each signup/<countrycode>/ then displays the same page (example.com/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.com/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?

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
    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
    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.com/?country=india you would access $_GET['country'] to find out that "india" has been requested.
    – WebElaine
    Jun 7, 2019 at 19:00

1 Answer 1

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!!

6
  • 1
    Works perfectly thanks. It was the page_id bit that did it! I'll look at the Canonical bit tomorrow.
    – Josh
    Jun 7, 2019 at 20:48
  • Although looking at it now that I can see it - it might not be the best way to show what I wanted. I may have to rethink how to show this best/better - maybe I'll go back to showing flags next to the price or maybe one flag on the page itself. Was trying to replicate the netflix experience (as far as I can tell anyway) since I couldn't really find any articles on best practice for top level domains related to currencies with more than country using the same currency name/symbol
    – Josh
    Jun 7, 2019 at 20:58
  • Seems WP automatically adds canonical tags in each page so I don't think I need to do anything in that respect.
    – Josh
    Jun 8, 2019 at 11:28
  • "It can't be just /signup/ because that will always be 301 or 302 to /signup/country/" - Although, if the canonical is not set to /signup then you will need to handle the scenario of users possibly requesting the wrong country in the URL. What would Googlebot see (a predominantly US based "user")? I wouldn't have thought the user (or bot) would always be redirected - only if the country has been reliably determined should you be redirecting.
    – MrWhite
    Jun 8, 2019 at 20:32
  • 1
    Geo-location alone is not usually sufficient to redirect (particularly since you are displaying currency, not country). So, if the "country" (or "currency"?) has been reliably determined then presumably this is already stored in the session (cookie based I assume). Googlebot won't have an active session (as any first time visitor), so shouldn't be redirected. (?) Although "redirecting" to modify the URL doesn't feel like the right thing to do (why not use JS pushState?) - the country/currency should ideally be present in the URL the users follows to begin with.
    – MrWhite
    Jun 8, 2019 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.