How to use rewrite rule or rewrite endpoint to switch languages?

You cannot do this by just adding a rewrite rule, you need to change every recorded rules. Here is a simple way to do this: function my_rewrite_rules($rules) { $new_rules = array(); $new_rules[‘(?:de|en)/?$’] = ‘index.php’; foreach ($rules as $key => $val) { $key = ‘(?:de|en)/?’ . $key; $new_rules[$key] = $val; } return $new_rules; } add_filter(‘rewrite_rules_array’, ‘my_rewrite_rules’, … Read more

Uppercase to Lowercase in URL

This is NOT mod_rewrite rule, it’s WordPress rewrite rule and it proposes to parse variables from URL to wp_query (most of the time). You can apply your logic during parse_query filter hook., something like this. add_filter( ‘parse_query’, function( WP_Query $wp_query ) : WP_Query { foreach ( [ ‘symbol’, ‘exchange’ ] as $key ) { if … Read more

Associate the “add_rewrite_endpoint” and “$_GET”

Query parameters added using add_rewrite_endpoint() are available using get_query_var(). So if you register /en/ as an endpoint: function wpse_318560_en_endpoint(){ add_rewrite_endpoint(‘en’, EP_ALL); } add_action(‘init’, ‘wpse_318560_en_endpoint’ ); And don’t flush rewrite rules on init. Rewrite rules only need to be flushed once. See this note in the developer docs for a way to properly flush rewrite rules … Read more

add_rewrite_endpoint not working

Your code should (and does from quick check) work in principle. You should not be flushing permalink every page load, just once. Use rewrite helper like Monkeyman Rewrite Analyzer to see if rules are being generated, what they are, and how are they matching to real URLs in your site.

add_rewrite_rule isnt working, not getting added to rules array, why?

Try this. You also don’t want to include $wp_rewrite->flush_rules() inside your function as that would flush on every page load. add_action(‘rewrite_rules_array’, ‘new_rewrite_rules’); function new_rewrite_rules($rules){ $newrules = array(); $newrules[ ‘^lists/([^/]*)/([^/]*)/?’ = ‘index.php?lists=$matches[1]&post=$matches[2]’; return $newrules + $rules; } If you want to flush the rules only once then try this: add_action(‘init’, ‘flush_new_rule’); function flush_new_rule(){ global $wp_rewrite; if(is_array(get_option(‘rewrite_rules’)) … Read more

add_rewrite_rule confusion

Expanding on my comment: The Rewriting API is for rewriting URLs, not redirecting. So the URL won’t change. You’re just telling WordPress that /testpage/ should load page_id 81, but the URL will stay /testpage/. Regardless, I tried the below, (where 501 is an ID that exists on my install) and it worked fine, loading that … Read more

Where do I USE add_rewrite_rule?

add_rewrite_rule is for converting a URL structure to query vars and routing requests through index.php, you can’t point to other URLs and use flags like you would in htaccess rewrites. for what you’re trying to achieve, doing it via htaccess is the simplest way. your rule should work if you just remove the leading slash: … Read more