I created this filter with the intention of creating URLs for the AMP version of my website:
function r_amp_add_rewrite_rules() {
global $wp_rewrite;
add_rewrite_rule('([^/]+)/amp/?$',
'index.php?amp=1&category_name=$matches[1]',
'top');
add_rewrite_rule('([^/]+)/amp/'.$wp_rewrite->pagination_base.'/?([0-9+])/?$',
'index.php?amp=1&category_name=$matches[1]&paged=$matches[2]',
'top');
add_rewrite_rule('([^/]+)/([^/]+)/amp/?$',
'index.php?amp=1&category_name=$matches[1]/$matches[2]',
'top');
add_rewrite_rule('([^/]+)/([^/]+)/amp/'.$wp_rewrite->pagination_base.'/?([0-9]{1,})/?$',
'index.php?amp=1&category_name=$matches[1]/$matches[2]&paged=$matches[3]',
'top');
add_rewrite_rule('([^/]+)/amp/?$',
'index.php?amp=1&pagename=$matches[1]',
'top');
add_rewrite_rule('amp/?$',
'index.php?amp=1&page_id=7',
'top');
}
add_filter('init', 'r_amp_add_rewrite_rules', 9999);
function r_amp_query_vars( $vars ) {
array_push($vars, 'amp');
return $vars;
}
add_filter( 'query_vars','r_amp_query_vars' );
Everything works fine, except one thing... I have Yoast SEO installed, with the option to remove the prefix "category" from the slugs of the categories and I also have categories with parent, ie categories with urls of this type:
/slug-main/name-category-child/
With the yoast seo option to remove the /category/ AMP url gives error 404.
/slug-main/name-category-child/amp/
And with the option deactivated, there is no error, but I want to remove the prefix category...
/category/slug-main/name-category-child/amp/
What can I do about it?
I have tried to install this plugin: https://wordpress.org/plugins/accelerated-mobile-pages/ and try if the same thing happened, and yes, it also happens... Although I want to fix it in my code, since I'm doing the AMP version of my theme without plugins...
Thanks.