最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - My WP_options db rewrite_rules Does Not Work

programmeradmin1浏览0评论

On my site, I have my own custom php file that handles URL GET requests. For example, at mysite/?burger=WellDone, it will query my db and display a page with all burgers with "WellDone" in the appropriate db field.

What I am trying to do is make it accessible via mysite/burger/WellDone, and after 5 hours straight, have been unsuccessful.

I have tried plugins, I have tried rewriting the htaccess, I have tried denying write permission to htaccess, and at current state, I am using a "rewrite" plugin by takien.

Before the plugin, accessing mysite/burger/WellDone would just show page not found. Now after entering my regex in (below) in the rewrite plugin, it adds my regex to the beginning of wp_options db rewrite_rules, and when I navigate to mysite/burger/WellDone, it now redirects me, but to the index of "mysite" rather than to mysite/?burger=WellDone.

Here is my regex and match:

^burger/([a-zA-Z0-9_/-/s]+)/?$
index.php?Burger=$matches[1]

As stated above, Entering my URL as mysite/?burger=WellDone displays perfectly as expected, but mysite/burger/WellDone just loops back to the homepage.

Edit:

Thanks to Welcher, I have added this code to functions.php, but it still redirecting to index:

function add_burger_query_vars_filter( $vars ){
  $vars[] = "burger";
  return $vars;
}
add_filter( 'query_vars', 'add_burger_query_vars_filter' );

function custom_rewrite() {
        $this_burger = get_query_var('burger');
add_rewrite_rule(
    '^burger/([a-zA-Z0-9_/-/s])/?$',
    'index.php?Burger='.$this_burger,           
    'top'
); //*/
}
add_action('init', 'custom_rewrite');

On my site, I have my own custom php file that handles URL GET requests. For example, at mysite/?burger=WellDone, it will query my db and display a page with all burgers with "WellDone" in the appropriate db field.

What I am trying to do is make it accessible via mysite/burger/WellDone, and after 5 hours straight, have been unsuccessful.

I have tried plugins, I have tried rewriting the htaccess, I have tried denying write permission to htaccess, and at current state, I am using a "rewrite" plugin by takien.

Before the plugin, accessing mysite/burger/WellDone would just show page not found. Now after entering my regex in (below) in the rewrite plugin, it adds my regex to the beginning of wp_options db rewrite_rules, and when I navigate to mysite/burger/WellDone, it now redirects me, but to the index of "mysite" rather than to mysite/?burger=WellDone.

Here is my regex and match:

^burger/([a-zA-Z0-9_/-/s]+)/?$
index.php?Burger=$matches[1]

As stated above, Entering my URL as mysite/?burger=WellDone displays perfectly as expected, but mysite/burger/WellDone just loops back to the homepage.

Edit:

Thanks to Welcher, I have added this code to functions.php, but it still redirecting to index:

function add_burger_query_vars_filter( $vars ){
  $vars[] = "burger";
  return $vars;
}
add_filter( 'query_vars', 'add_burger_query_vars_filter' );

function custom_rewrite() {
        $this_burger = get_query_var('burger');
add_rewrite_rule(
    '^burger/([a-zA-Z0-9_/-/s])/?$',
    'index.php?Burger='.$this_burger,           
    'top'
); //*/
}
add_action('init', 'custom_rewrite');
Share Improve this question edited Apr 25, 2017 at 5:02 Chris asked Apr 25, 2017 at 2:46 ChrisChris 114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The rewrite is taking you back to the homepage with the Burger param but the url is being rewritten, so you aren't seeing it.

You should be able to retrieve the value of Burger with get_query_var('Burger') which will contain the value of $matches[1]

You can read more about get_query_var in the codex

$burger = get_query_var( 'Burger');
// $burger will have the string "welldone" in it'
发布评论

评论列表(0)

  1. 暂无评论