admin管理员组文章数量:1200385
I have urls of this form:
example/event/?eventId=1 example/event/?eventId=2
The problem is Google is indexing only example/event, which is a blank page. It's not able to display an event without the Id. I would like to redirect this url to this form:
example/event/1
This will allow Google to index each event individually.
The event page is provided by the admin. I imagine something like:
function add_eventId_rule() {
$post = get_post(get_option('EVENT_AGENT_EVENT_PAGEID'));
if($post != null)
{
add_rewrite_rule(
substr(wp_make_link_relative(get_permalink($post)), 1) . '(\d*)/?$',
'index.php?page_id=' . $post->ID .'&eventId=$matches[1]',
'top'
);
// this produces the regex event/(\d*)/?$ Can produce foo/bar/event if
// the display page has parent pages
}
}
function ea_query_vars($qvars) {
$qvars[] = 'eventId';
return $qvars;
}
function capture_event_display_page($post)
{
add_option('EVENT_AGENT_EVENT_PAGEID', $post->ID);
flush_rewrite_rules(true);
}
add_filter( 'query_vars', 'ea_query_vars' );
add_action( 'init', 'add_eventId_rule');
add_action( 'save_post', 'capture_event_display_page')
Using this code, the following work: /?eventId=80 .php?page_id=87&eventId=80
But the one I want doesn't. It strips the eventId:
How come? What am I missing?
I have urls of this form:
example.com/event/?eventId=1 example.com/event/?eventId=2
The problem is Google is indexing only example.com/event, which is a blank page. It's not able to display an event without the Id. I would like to redirect this url to this form:
example.com/event/1
This will allow Google to index each event individually.
The event page is provided by the admin. I imagine something like:
function add_eventId_rule() {
$post = get_post(get_option('EVENT_AGENT_EVENT_PAGEID'));
if($post != null)
{
add_rewrite_rule(
substr(wp_make_link_relative(get_permalink($post)), 1) . '(\d*)/?$',
'index.php?page_id=' . $post->ID .'&eventId=$matches[1]',
'top'
);
// this produces the regex event/(\d*)/?$ Can produce foo/bar/event if
// the display page has parent pages
}
}
function ea_query_vars($qvars) {
$qvars[] = 'eventId';
return $qvars;
}
function capture_event_display_page($post)
{
add_option('EVENT_AGENT_EVENT_PAGEID', $post->ID);
flush_rewrite_rules(true);
}
add_filter( 'query_vars', 'ea_query_vars' );
add_action( 'init', 'add_eventId_rule');
add_action( 'save_post', 'capture_event_display_page')
Using this code, the following work: https://www.eventagent.ai/event/?eventId=80 https://www.eventagent.ai/index.php?page_id=87&eventId=80
But the one I want doesn't. It strips the eventId: https://www.eventagent.ai/event/80
How come? What am I missing?
Share Improve this question edited Apr 27, 2022 at 2:52 AldenG asked Apr 21, 2022 at 22:49 AldenGAldenG 114 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 0Because it's not enough to register your rule, you need to flush the rewrite rules for it to take effect. An easy way of doing this is to visit the permalinks settings page.
This is true regardless of where the rewrite rule came from. The most common reason people encounter this is because of the rewrite rules core creates for custom post types, but this is not unique to custom post types.
Another possibility is that your rewrite rules regular expression doesn't match the URLs you are testing, or, that another rule with higher priority matches it first. The rewrite analyser plugin or one of its competitors can help diagnose this.
Warning: A common mistake developers make to try and work around this is to flush the rewrite rules programmatically on every request so they don't have to do it manually. This introduces bugs but it also adds a heavy performance hit that's trivially avoided. Flush them on plugin de/activation, when you update your plugins database settings, or when user interaction requires it, and never on the frontend unless absolutely unavoidably necessary. You may see this referred to as flushing on the init
hook.
本文标签: url rewritingRewrite parameters as Url for SEO
版权声明:本文标题:url rewriting - Rewrite parameters as Url for SEO 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738611383a2102651.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
example.com/foo/bar
, run it through rewrite rules ( regexes ) that map it into the formindex.php?foo=bar
then it processes it. In that examplefoo
is a query variable. That misunderstanding may be why you haven't found your solution. When you add/remove a rewrite rule you need to re-save permalinks as that's the easiest way to flush all the rules out and recreate them. The rewrite rules analyser plugin lets you type the URL in and see which rules match and in which order. Maybe you didn't re-save or whitelist eventId – Tom J Nowell ♦ Commented Apr 25, 2022 at 14:22