1

On my site, I have my own custom php file that handles URL GET requests. For example, at mysite.com/?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.com/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.com/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.com/burger/WellDone, it now redirects me, but to the index of "mysite.com" rather than to mysite.com/?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.com/?burger=WellDone displays perfectly as expected, but mysite.com/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');
0

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'
| improve this answer | |
  • Is there someway to prevent the value of Burger from being rewritten? If so, how? Otherwise, where should I add the get_query_var in order to pass it back to matches[1]? Should I be including all of this in an add_rewrite_rule? – Chris Apr 25 '17 at 3:11
  • The point of a rewrite is to have nice, readable URLS - site.com/burder/welldone is much better than site.com?Burger=welldone. The value of Burger is already in $matches[1] - calling get_query_var will return whatever it is - in this case, it's the string welldone – Welcher Apr 25 '17 at 3:15
  • In response to this and your edit, I understand the pieces I need, now I am trying to understand how to incorporate them. I currently have no regex php referencing burger; the regex and match I provided in the original question are directly in the wp_options rewrite_rules database. I suppose what Im asking in followup is, how do I incorporate this rewrite rule properly (including the get_query_var), and where? – Chris Apr 25 '17 at 3:32
  • I've tried adding rewrite rules and tags in functions.php, index.php, etc, nothing is sticking... – Chris Apr 25 '17 at 5:26

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.