I've been trying to achieve a seemingly simple permalink structure customization without success. The goal is to always have /aa-bb/
in front of each possible permalink, where aa is a language code and bb a country code. Whatever comes after this should behave exactly like it would normally. So where normally you'd have /post-title/
, afterwards you'd have /en-gb/post-title/
and it would lead to the same post (including all variations such as pages, CPTs, archives, categories, search etc.).
The complete flow:
- I generate a list of valid language/country values = complete
- I retrieve the value from the URL to determine the language/country = complete
- During the template_redirect action hook I redirect to the default language/country if no valid value is present = complete
- Set up a permalink rewrite structure to process these URLs as if the first part didn't exist = mystery
All methods for rewriting WP permalinks seem to do something slightly different from this goal. Is this something that would be better done with htaccess? WPML seems to do it successfully with their language codes but I'm not sure how.
Using add_rewrite_rule sure works, but only covers one permalink type at a time so I'm afraid of falling short if I'd manually add all variations.