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

Wordpress add a rewrite rule to a page to accept a GET variable

programmeradmin4浏览0评论

I want to rewrite

mysite/events/submission/test-event-01

to

mysite/events/submission?epl=test-event-01

This is what I have done so far without any result. I get a 404 error (mysite/events/submission/test-event-01). Can anyone suggest the correct rule?

add_action( 'init', 'epl_rewrite');
add_filter( 'query_vars', 'epl_query_vars');
        
function epl_rewrite()
{
    add_rewrite_rule('^submission/([^/]*)[/]?', 'index.php?pagename=submission&epl=$matches[1]', 'top');
}
        
function epl_query_vars( $query_vars )
{
    $query_vars[] = 'epl';
    return $query_vars;
}

Note:

  1. Permalinks are re-saved to flush the rewrite rules.
  2. mysite/events/submission page is already added.

Thank you in advance.

I want to rewrite

mysite.com/events/submission/test-event-01

to

mysite.com/events/submission?epl=test-event-01

This is what I have done so far without any result. I get a 404 error (mysite.com/events/submission/test-event-01). Can anyone suggest the correct rule?

add_action( 'init', 'epl_rewrite');
add_filter( 'query_vars', 'epl_query_vars');
        
function epl_rewrite()
{
    add_rewrite_rule('^submission/([^/]*)[/]?', 'index.php?pagename=submission&epl=$matches[1]', 'top');
}
        
function epl_query_vars( $query_vars )
{
    $query_vars[] = 'epl';
    return $query_vars;
}

Note:

  1. Permalinks are re-saved to flush the rewrite rules.
  2. mysite.com/events/submission page is already added.

Thank you in advance.

Share Improve this question asked Feb 27, 2022 at 21:20 sariDonsariDon 2651 gold badge2 silver badges18 bronze badges 2
  • GET variables aren't a part of the pretty permalinks/rewrite rules system, and you don't need to register GET variables as query vars ( not unless you intend to map part of a pretty URL onto it ). If you want people who visit mysite.com/events/submission/test-event-01 to instead see mysite.com/events/submission?epl=test-event-01 then that's not a rewrite, that's a redirect. WP Rewrite rules are for mapping pretty URLs on to ugly URLs of the form index.php?foo=bar, they aren't redirects. – Tom J Nowell Commented Feb 27, 2022 at 22:51
  • Thank you so much for the clarification. I will keep a note on this. My purpose is now served with get_query_var. – sariDon Commented Feb 28, 2022 at 5:12
Add a comment  | 

1 Answer 1

Reset to default 0

Since 'submission' is a child page of 'events', the rewrite rule should include the parent path

add_rewrite_rule('events/([^/]*)/submission/?', 'index.php?pagename=events/submission&epl=$matches[1]', 'top');
发布评论

评论列表(0)

  1. 暂无评论