admin管理员组文章数量:1425789
Please I recently changed my site permalink structure, from / to /
I have searched online and even here but all I am seeing is /postdate/year/postname/ to /postname/
Since I have many categories I cannot make an htaccess 301 redirection for this, please is there any other way this can be done?
Please I recently changed my site permalink structure, from https://example/postname/ to https://example/category/postname/
I have searched online and even here but all I am seeing is /postdate/year/postname/ to /postname/
Since I have many categories I cannot make an htaccess 301 redirection for this, please is there any other way this can be done?
Share Improve this question asked May 28, 2019 at 12:34 Godwin Alex OgbondaGodwin Alex Ogbonda 1192 silver badges13 bronze badges 4 |1 Answer
Reset to default 1I assume you have added new permalink structure by defining Custom Permalink Structure on
https://yoursite/wp-admin/options-permalink.php
and added /%category%/%postname%/ in the custom structure field. This is the WordPress default/suggested method.
After this, you need to flush the old permalink structure by saving and reloading the permalink settings page.
This will not work for custom post type and only works for WordPress default post type. For custom post types you would have to apply additional hooks or defile it while registering custom post type function.
You should read the WordPress official documentation on using permalinks.
Update:
In this case, you can use "404_template" filter.
Example:
add_filter( '404_template', 'custom_redirect_to_category' );
function custom_redirect_to_category($template) {
if ( ! is_404() ){
return $template;
}
global $wp_rewrite;
global $wp_query;
if ( '/%category%/%postname%/' !== $wp_rewrite->permalink_structure ){
return $template;
}
if ( ! $post = get_page_by_path( $wp_query->query['category_name'], OBJECT, 'post' ) ){
return $template;
}
$permalink = get_permalink( $post->ID );
wp_redirect( $permalink, 301 );
exit;
}
本文标签: categoriesRedirect Changed Permalink on wordpress
版权声明:本文标题:categories - Redirect Changed Permalink on wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449566a2658822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
template_redirect
action for redirects. In your case it could be summarized as: check if the URL has a post slug, if that post slug exists in the db, redirect to the new permalink – kero Commented May 28, 2019 at 14:04