I want to be able to have a url that has a region defined in it by country (aka USA = us or Canada = ca) and I want the url to be pretty. None of this attaching variables onto the url.

When designing a WordPress page there is the slug field which basically is the url for that page.


I want to add the country code to the url in my theme and allow php to sort out my choice without getting a 404. Or due to canonical urls the ca or us magically point to other pages. (ie. if ending the url with country code ca, the site redirects to /careers page, which is not what I want.)

Wish I had an example to provide, my site is not live yet. Still working in a dev state.

Is there a way I can modify the *.htaccess to customize the rules for my needs? Or is there another way that I can resolve this issue? I considered internationalization, but there have been suggested image/layout changes.


Using add_rewrite_rule as declared in functions.php

  function custom_rewrite_rule() {
    //student page_id = 95
  add_action('init', 'custom_rewrite_rule', 10, 0);

How to I access the country? Is it via $_GET['country']?

  • the simplest way is probably with a rewrite endpoint.
    – Milo
    Jan 16, 2017 at 18:24
  • @Milo when doing that technique do I need to create a rewrite_endpoint for all pages? I'll create and example now and add it to the above under update. Note I am trying to implement it using reg expressions.
    – mcv
    Jan 16, 2017 at 19:11

1 Answer 1


Referencing this question I was able to resolve this issue:

Need help with add_rewrite_rule

To briefly display the changes I made here it goes, in my theme functions.php I added the following action:

function custom_rewrite_rule(){

This above allows for my urls to be the pretty form of:

  • {{site_url}}/student/ca/
  • {{site_url}}/student/us/

However this form of url is not recognizable until the permalinks are flushed. To do so go to your wp-admin dashboard, select Settings->Permalinks from the right hand menu screen and press the Save button at the bottom of the main screen.

Now the above urls should work.

Next you want to save the additional var in the url for later use. In the above example it is country. This is done using add_filter in functions.php:

function country_selection($query_vars){
    return $query_vars;

Lastly you might want to access this value later in pages (ie: student.php). To do so use the following:


You can echo / var_dump the above variable or store it's result in another variable for later use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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