是否有人可以为我重定向以下永久链接URL?
WordPress的URL为:http://example.com/?attachment_id=411
我想将其重定向到新结构I do it,以下代码在旧URL中不适用于我:
/* add new rewrite rule */
function attachment_rewrite( $wp_rewrite ) {
$rule = array(
\'media/(.+)\' => \'index.php?attachment=\' . $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $rule + $wp_rewrite->rules;
}
add_filter( \'generate_rewrite_rules\', \'attachment_rewrite\' );
/* redirect standard wordpress attachments urls to new format */
function redirect_old_attachment() {
global $wp;
if( !preg_match( \'/^media\\/(.*)/\', $wp->request ) && isset( $wp->query_vars[\'attachment\'] ) ) {
wp_redirect( site_url( \'/media/\' . $wp->query_vars[\'attachment\'] ) , 301 );
exit();
}
}
add_filter( \'template_redirect\', \'redirect_old_attachment\' );
/**/
function wpd_attachment_link( $link, $post_id ){
$post = get_post( $post_id );
return home_url( \'/media/\' . $post->post_name );
}
add_filter( \'attachment_link\', \'wpd_attachment_link\', 20, 2 );
所以我们需要什么时候开门
http://example.com/?attachment_id=411
, 将其重定向到
http://example.com/media/post-title