$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 - add_rewrite_rule ignoring other params than the "p" param|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 - add_rewrite_rule ignoring other params than the "p" param

matteradmin8PV0评论

A specific post on my website has a shortcode that uses a parameter called "query" to produce its content. So, this is an example of a working URL:


"my-post" is the permalink of the post and the page is using the "query" param correctly.

However, we need to change this URL to a friendly one like and then I was trying to use the add_rewrite_rule like this:

function custom_add_rewrite_rules() {
    add_rewrite_rule(
        '^something-here/([\d]+)$',
        'index.php?p=10219&query=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_add_rewrite_rules');

When I try to access the friendly URL, I'm redirected to successfully, but the query param is null ($_GET["query"] = null).

I also tried making use of add_rewrite_tag to register "query", but with no success.

If anyone has some light to shed it'd be more than appreciated.

A specific post on my website has a shortcode that uses a parameter called "query" to produce its content. So, this is an example of a working URL:

https://example/my-post?query=980

"my-post" is the permalink of the post and the page is using the "query" param correctly.

However, we need to change this URL to a friendly one like https://example/something-here/980 and then I was trying to use the add_rewrite_rule like this:

function custom_add_rewrite_rules() {
    add_rewrite_rule(
        '^something-here/([\d]+)$',
        'index.php?p=10219&query=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_add_rewrite_rules');

When I try to access the friendly URL, I'm redirected to https://example/my-post successfully, but the query param is null ($_GET["query"] = null).

I also tried making use of add_rewrite_tag to register "query", but with no success.

If anyone has some light to shed it'd be more than appreciated.

Share Improve this question asked Nov 8, 2018 at 18:33 Adriano CastroAdriano Castro 1991 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

It sounds like the plugin expects there to be a get param.

Your rewrite-rule is removing the get-param before it can be used by the plugin.

If the plugin hasn't been updated for a long time (it's abandonware) then you can go through the plugin and replace the get-param with the portion of the url which would have been the get-param. Just make sure to prevent the plugin from updating after you've made your edits.

If you add the following

function custom_add_rewrite_rules() {
  add_rewrite_tag('%query%','([^&]+)');

  add_rewrite_rule(
      '^something-here/([\d]+)$',
      'index.php?p=10219&query=$matches[1]',
      'top'
  );
}
add_action('init', 'custom_add_rewrite_rules');

& then replace $_GET['query'] &/or $_REQUEST['query] with get_query_var( 'query' ) in the plugin you've described, that should be enough to do the trick.

Stephen Harris sums it up nicely in the answer here

Post a comment

comment list (0)

  1. No comments so far