I have tried to rewrite the url from /?state=AL
to
Added query vars and rewrite url in functions.php
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'club';
return $vars;
}
function custom_rewrite_basic()
{
add_rewrite_tag("%club%", "([a-z0-9\-_]+)");
add_rewrite_rule('^club/([a-z0-9\-_]+)/?$', 'index.php?pagename=club&State=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
I have tried to rewrite the url from http://example.com/club/?state=AL
to http://example.com/club/AL
Added query vars and rewrite url in functions.php
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'club';
return $vars;
}
function custom_rewrite_basic()
{
add_rewrite_tag("%club%", "([a-z0-9\-_]+)");
add_rewrite_rule('^club/([a-z0-9\-_]+)/?$', 'index.php?pagename=club&State=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Share
Improve this question
asked Feb 11, 2022 at 18:52
mikemike
112 bronze badges
1
|
1 Answer
Reset to default 0You can create an Apache .htaccess internal rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^state\=AL$
RewriteRule ^club/$ /club/AL? [L]
club
, notState
, you would also need to modify your page to read in the value usingget_query_var
not$_GET
and youradd_rewrite_tag
call is unnecessary. Also did you re-save your permalinks after making these changes, and have you tested your URL using the rewrite rule analyser plugin? Did you change your links to point to the new pretty permalinks instead? ( WP won't do that for you ) – Tom J Nowell ♦ Commented Feb 11, 2022 at 20:56