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.
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 triedadd_rewrite_rule('^signup/([^/]*)/','index.php?pagename=signup','top');
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/
orsignup/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.
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 triedadd_rewrite_rule('^signup/([^/]*)/','index.php?pagename=signup','top');
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/
orsignup/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 |1 Answer
Reset to default 1If 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
$_GET
for anything in the query string, so if your URL ishttp://example/?country=india
you would access$_GET['country']
to find out that "india" has been requested. – WebElaine Commented Jun 7, 2019 at 19:00