I'm trying to rewrite URLs on my wordpress site to match Googles "seo friendly" policy. I want to rewrite URL's for 3 types of queries generated from search plugin:

a) Location


to be:


where london can be any location based on user search criteria.

b) Location + brand:


to be:


where london and audi can be any location/brand based on user search criteria.

c) Location + brand + model:


to be:


where london, audi and audi a1 series can be any location/brand/model based on user criteria.

Search results URLs displays nonce which is different for each user. I want to skip it from "friendly URL's" so I can index these URL's in Google.

1. I have tried, without success:

Rule Pattern:




2. Following https://codex.wordpress.org/Rewrite_API/add_rewrite_rule I have tried to test Custom Rewrites and I added the following code to my functions.php file:

function custom_rewrite_basic() {
  add_rewrite_rule('^testrewrite/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
add_action('init', 'custom_rewrite_basic');

I have flushed and regenerated the rewrite rules database but test rewrite seams not working as accessing http://example.com/testrewrite/38 returns 404.

3. Mod_Rewrite is enabled.

sudo a2enmod rewrite 
Module rewrite already enabled

Any advice? :)

1 Answer 1


Try to add this one

function add_rewrite_rules() {
    $newrules = array('^testrewrite/([0-9]+)/?'=> 'index.php?page_id=$matches[1]');

    return $newrules ;
add_filter('rewrite_rules_array', 'add_rewrite_rules');

Let me know it is working or not.

  • Adding your code to functions.php solved problem number 2, I can now access example.com/testrewrite/38 Unfortunately this makes my other posts/pages returns 404 :-( E.g. example.com/auto/… returns 404.
    – Kris
    Jan 22, 2019 at 12:27
  • You can add it multiple there by separating ' , ' as this is an array add your condition adter , like $newrules = array('^testrewrite/([0-9]+)/?'=> 'index.php?page_id=$matches[1]','^used-cars/([^/]*)/([^/]*)/([^/]*)/?'=> 'index.php?page_id=$matches[1]'); Jan 22, 2019 at 12:32

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.