$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Custom rewrite not working|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Custom rewrite not working

matteradmin8PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far