1

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

example.com/used-cars/?style1_nonce=5bcd0fed69&location=london&autobrand=&automodel=&modelyear=&fueltype=&priceRange=540%2C43000&autotype=&searchauto=

to be:

example.com/used-cars/london/

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

b) Location + brand:

example.com/used-cars/?style1_nonce=5bcd0fed69&location=london&autobrand=audi&automodel=&modelyear=&fueltype=&priceRange=540%2C43000&autotype=&searchauto=

to be:

example.com/used-cars/london/audi/

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

c) Location + brand + model:

example.com/used-cars/?style1_nonce=5bcd0fed69&location=london&autobrand=audi&automodel=AUDI+A1&modelyear=&fueltype=&priceRange=540%2C43000&autotype=&searchauto=

to be:

example.com/used-cars/london/audi/audi+a1/

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:

^used-cars/([^/]*)/([^/]*)/([^/]*)/?

Match:

index.php?style1_nonce=$matches[1]&location=$matches[2]autobrand=$matches[3]&automodel=$matches[4]&modelyear=&fueltype=&priceRange=540%2C43000&autotype=&searchauto=

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

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 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]'); – Tejas Gajjar Jan 22 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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