Rewrite parameters as Url for SEO
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=87eventId=80
But the one I want doesn't. It strips the eventId: https://www.eventagent.ai/event/80
How come? What am I missing?
Topic url-rewriting Wordpress
Category Web