admin管理员组文章数量:1134247
I am using the following code to rewrite "example?my_var=test" to "example/my_var/test":
function my_query_vars($vars)
{
$vars[] = 'my_var';
return $vars;
}
function my_rewrite()
{
add_filter('query_vars', 'my_query_vars');
add_rewrite_tag('%my_var%', '([^&]+)');
add_rewrite_rule('^my_var/(.+)/?$', 'index.php?page_id=16&my_var=$matches[1]', 'top');
}
add_action('init', 'my_rewrite');
This only works, if the page with id 16 is not set as "page_on_front" in the WordPress settings.
- "page_on_front" = whatever --> above code works
- "page_on_front" = 16 --> "example/my_var/test" redirects to "example"
Do you have an idea why this is happening?
thanks a lot, Jan
EDIT
I disabled the redirect with this code and it seems to work just fine:
function disable_redirect($redirect_url, $requested_url)
{
if ( get_query_var( 'my_var' ) )
{
return '';
}
return $redirect_url;
}
function my_query_vars($vars)
{
$vars[] = 'my_var';
return $vars;
}
function my_rewrite()
{
add_filter('redirect_canonical', 'disable_redirect', 10, 2);
add_filter('query_vars', 'my_query_vars');
add_rewrite_tag('%my_var%', '([^&]+)');
add_rewrite_rule('^my_var/(.+)/?$', 'index.php?page_id=16&my_var=$matches[1]', 'top');
}
add_action('init', 'my_rewrite');
Do you think this is a good solution? Is there a better way to disable the redirect?
本文标签: phpWordPress addrewriterule not working with pageonfront
版权声明:本文标题:php - WordPress add_rewrite_rule not working with page_on_front 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736857844a1955791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论