• Hi, I am trying to change the permalink structure of blog posts only for the future posts without changing the old URLs due to their SEO scores.

    When i try to change the permalink as https://www.domain.com/blog/catname/postname it works:

    // rewriting post url
    function format_post_url( $post_link, $post, $leavename ) {
    	 if ( 'post' == get_post_type($post) && 'publish' == $post->post_status && strtotime('2017-04-01 00:00:00') < strtotime($post->post_date) ) { 
    
    		$post_link = rtrim($post_link,"/");
    
    		$post_pos = strrpos($post_link,"/");
    		$post = substr($post_link, $post_pos+1); // post name
    		
    		$post_link = substr($post_link, 0,strlen($post_link)-strlen($post)-1);
    
    		$cat_pos = strpos($post_link,"/",8);
    		$cat = substr($post_link, $cat_pos+1); // category name
    
    		if(substr($cat,0,3)=="en/"){
    			$cat = substr($cat, 3);
    		}
    
    		$post_link = substr($post_link, 0,strlen($post_link)-strlen($cat)-1);
    
    		return $post_link."/blog/".$cat."/".$post."/";
    
    	}
    	return $post_link;
    }
    
    add_filter( 'post_link', 'format_post_url', 100, 3 );

    But when i try to change the permalink as https://www.domain.com/postname it gives a 404 error, like following:

    return $post_link."/".$post."/";

    Please note that i can achieve this easily by changing the permalink structure via the WP backend but it will alter all the URLs, past & future, but i just want to change the future ones.

    Can you guys please help me ? I even update the permalink structure settings in the wp backend after making these changes but it still doesn’t work.

Viewing 1 replies (of 1 total)
  • I’m afraid this is not easily done, since the permalink structure is a site-wide option.
    When I faced the same situation, the easiest solution turned to be transferring link value via 301 redirects for all old posts into the new permalink structure.
    I simply made a list of old vs. new addresses and put a row for each changed address in .htaccess, like this:
    redirect 301 /image1 https://site.com/text/191/image1/

    If you don’t introduce any new elements in the structure, then a rewrite rule with permanent readdressing may be better (where you typically would end the entry with [L,R=301]). There are many sources on the Internet about 301 redirect using mod.rewrite.

Viewing 1 replies (of 1 total)
  • The topic ‘Change permalink structure only for future posts’ is closed to new replies.