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

functions - How to rewrite the custom url in wordpress?

programmeradmin0浏览0评论

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
  • Your query var that you added is named club, not State, you would also need to modify your page to read in the value using get_query_var not $_GET and your add_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
Add a comment  | 

1 Answer 1

Reset to default 0

You can create an Apache .htaccess internal rewrite

RewriteEngine on
RewriteCond %{QUERY_STRING} ^state\=AL$
RewriteRule ^club/$ /club/AL? [L]
发布评论

评论列表(0)

  1. 暂无评论