Append country to ‘pretty’ url but serve same page

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