url rewriting - Custom rewrite not working

admin2025-06-04  2

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/used-cars/?style1_nonce=5bcd0fed69&location=london&autobrand=&automodel=&modelyear=&fueltype=&priceRange=540%2C43000&autotype=&searchauto=

to be:

example/used-cars/london/

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

b) Location + brand:

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

to be:

example/used-cars/london/audi/

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

c) Location + brand + model:

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

to be:

example/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 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 returns 404.

3. Mod_Rewrite is enabled.

sudo a2enmod rewrite 
Module rewrite already enabled

Any advice? :)

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/used-cars/?style1_nonce=5bcd0fed69&location=london&autobrand=&automodel=&modelyear=&fueltype=&priceRange=540%2C43000&autotype=&searchauto=

to be:

example/used-cars/london/

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

b) Location + brand:

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

to be:

example/used-cars/london/audi/

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

c) Location + brand + model:

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

to be:

example/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/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/testrewrite/38 returns 404.

3. Mod_Rewrite is enabled.

sudo a2enmod rewrite 
Module rewrite already enabled

Any advice? :)

Share Improve this question asked Jan 22, 2019 at 12:14 KrisKris 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748986716a315404.html

最新回复(0)