Site icon Hip-Hop Website Design and Development

Rewrite rule to support multi-language page structure

I am trying to create a rewrite rule to support a multi-language URL structure in WordPress.

Using the French (/fr/) language to illustrate, I currently have the following rewrite rule:

function custom_rewrite() {
    add_rewrite_rule('^fr/(.*)$','index.php?pagename=$matches[1]','top');
}
add_action( 'init','custom_rewrite' );

This code is meant to rewrite any request with an "/fr/" slug immediately following the domain to be forwarded to the page as if the "/fr/" slug wasn’t there.

For example:

example.com/fr/blog would rewrite to example.com/blog

example.com/fr/abc/foo-bar/cool would rewrite to example.com/abc/foo-bar/cool

The code I posted above works for most cases but not all. I suspect it is because I am rewriting the request to index.php?pagename=$matches[1] where pagename is not the appropriate handle to use. I’m actually not entirely sure what pagename refers to in the context of params following index.php in WordPress or what the correct handle for the rewrite target would be.

P.S. I know there are many plugins to facilitate multi-language sites. I am tasked with doing this using a custom approach.

Any help appreciated!