Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redirects wordpress posts to new url: site.com/blog/post-name
<?php
/**
* Add new rewrite rule
*/
function create_new_url_querystring() {
add_rewrite_rule(
'blog/([^/]*)$',
'index.php?name=$matches[1]',
'top'
);
add_rewrite_tag('%blog%','([^/]*)');
}
add_action('init', 'create_new_url_querystring', 999 );
/**
* Modify post link
* This will print /blog/post-name instead of /post-name
*/
function append_query_string( $url, $post, $leavename ) {
if ( $post->post_type != 'post' )
return $url;
if ( false !== strpos( $url, '%postname%' ) ) {
$slug = '%postname%';
}
elseif ( $post->post_name ) {
$slug = $post->post_name;
}
else {
$slug = sanitize_title( $post->post_title );
}
$url = home_url( user_trailingslashit( 'blog/'. $slug ) );
return $url;
}
add_filter( 'post_link', 'append_query_string', 10, 3 );
/**
* Redirect all posts to new url
* If you get error 'Too many redirects' or 'Redirect loop', then delete everything below
*/
function redirect_old_urls() {
if ( is_singular('post') ) {
global $post;
if ( strpos( $_SERVER['REQUEST_URI'], '/blog/') === false) {
wp_redirect( home_url( user_trailingslashit( "blog/$post->post_name" ) ), 301 );
exit();
}
}
}
add_filter( 'template_redirect', 'redirect_old_urls' );
@dskvr

This comment has been minimized.

Copy link

commented Feb 21, 2017

👍

@jamesfacts

This comment has been minimized.

Copy link

commented Mar 24, 2017

Very helpful stuff, thanks!

@BooneSesvold

This comment has been minimized.

Copy link

commented Apr 13, 2018

Perfect solution. Easy way redirect all posts without affecting custom post types or having to manually 301. Thank you!

@vaheed426

This comment has been minimized.

Copy link

commented Mar 22, 2019

HI,

Thanks for code.

I was Modify post link /blog/post-name instead of /post-name with custom code.

Page working fine. But AMP pages not working. Getting 404 error

PLease check this links
https://www.whichbroker.com/en/news/city-index-brand-to-be-shelved/
https://www.whichbroker.com/en/news/city-index-brand-to-be-shelved/amp/

Can you please help me on this issue?

@carasmo

This comment has been minimized.

Copy link

commented May 27, 2019

This breaks with paginated single posts (<!--nextpage-->), adding this to the rewrite function fixes it.

    //corrects inpost navigation without breaking archive pagination `bottom`
    add_rewrite_rule(
        'blog/([^/]*)(/[0-9]+)?/?$',
        'index.php?name=$matches[1]&page=$matches[2]',
        'bottom'
    );

@carasmo

This comment has been minimized.

Copy link

commented May 27, 2019

Here is what I use:

https://gist.github.com/carasmo/d987754910efe2d7303e0be6e8068ea9

  1. Adds the blog/ path without affecting all other CPTS now and in the future.
  2. Changes all labels to Blog
  3. Converts post_tag to hierarchical because flat is annoying to deal with
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.