Custom rewrite rules for a $_GET request

Assuming I have this URL:

http://site.com/?get=something

How can I change it to a nice URL that looks like:

http://site.com/get_something

using WP’s URL rewriting system?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First add get to query vars array:

function add_query_vars_wpa12572($vars) {
    $vars[] = 'get'
    return $vars;
}

add_filter('query_vars', 'add_query_vars_wp12572');

then add the rewrite rule

function author_rewrite_rules_wpa12572( $wp_rewrite ) {
  $newrules = array();
  $new_rules['get_(d*)$'] = 'index.php?get=$matches[1]';
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules','author_rewrite_rules_wpa12572');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x