2

Is it possible to change permalinks on all posts created after a certain date? This because we don't want old URLs to break.

Now we have /%year%/%month%/%postname% We want to have /%postname

  • 2
    Keeping separate structures isn't the best idea. Imagine changing the structure in a while and then having to maintain three structures. Welcome maintainability madness! Just make sure you have proper 301 redirects, the effects you are probably worrying about won't be huge and won't last that long. – Nicolai Jun 14 '15 at 17:06
6
+25

Your permalink structure is global; it is not a property of individual posts. There is no way to indicate that one post uses a certain permalink structure while another uses a different one.

This makes sense when you think about how WordPress processes requests. Using the new structure, WordPress maps the request to index.php?name=$1 where $1 is the post slug portion of the request. WordPress then queries the database for a post with a slug that matches the request.

If every post had its own permalink structure, WordPress would have to iterate over every single post until it found one with a permalink structure that matched the request.

What you can do is redirect your old permalinks to the new permalinks. There are a couple of ways you can go about doing this:


.htaccess

You can add the following to your .htaccess file, before the WordPress rewrite rules:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^[0-9]{4}/[0-9]{2}/(.*)$ /$1 [R=301,L]
</IfModule>

This will match the year/month/postname structure and redirect to the postname structure.

add_rewrite_rule

You can use add_rewrite_rule to create an additional rule that matches your old permalink structure.

add_action('init', function() {
    flush_rewrite_rules();
    add_rewrite_rule('^([0-9]{4})/([0-9]{2})/(.*)$', 'index.php?name=$matches[3]', 'bottom');
});

This accomplishes the same thing as the .htaccess method. If you use this approach make sure you flush your rewrite rules by visiting the Settings > Permalinks page in your backend.


Regardless of which approach you use, the canonical URL for your posts will use the new permalink structure. Requests using the old permalink structure will be redirected to the new structure.

Search engines will eventually catch on to the change and index the new structure rather than the old structure. At that point, you can drop the additional rewrite rules, so long as you don't have any internal links using the old structure.

If you do have internal links using the old structure, I would use this utility to do a regex search and replace. Just make sure you make a backup of your database.

If you are concerned about inbound links using the old structure, there isn't much you can do other than keep the rewrite rules indefinitely.

  • Note: Actually most of the time there is no need for adding those rules manually, as - at least - redirect_canonical() and wp_old_slug_redirect() are taking care of that, with 301 redirects of course. This works mostly very well for all standard cases, one should always check if it works anyway. But this seems pretty much like one of the most of the time occurring cases, so its standard and should work. – Nicolai Jun 14 '15 at 16:59
  • @ialocin At first I thought WordPress would be able to map the old structure to the new automatically as well, but I ran a test and got a 404 using the year/month structure after I updated the permalinks. I believe wp_old_slug_redirect handles changes to the post slug, but not the permalink structure. redirect_canonical redirects to the canonical URL in the case that multiple rules map to a particular post. In my test case at least, I had to add the rules to get the old URLs to work. – Mathew Tinsley Jun 14 '15 at 17:03
  • Too bad, I mean this seems very much like a standard case. Aside from that, there is a reason why I said »most of the time« and »should work«. Otherwise I just wanted to note it, you have gotten my vote anyway. – Nicolai Jun 14 '15 at 17:15
0

Depending on how many pages that represents (the old ones), you might consider creating a rewrite rule for every single page with a permanent redirect flag.

If this is not an option, I think mtinsley's answer is your best bet.

0

You might find the Redirection plugin interesting.

It should allow you to change the permalink structure while keeping old URLs valid.

-1

You can create a new post type, under the name "post" and then add a rewrite slug. This will overwrite the default Wordpres post type.

Here is a sample code:

add_action( 'init', 'my_new_default_post_type', 1 );
function my_new_default_post_type() {

    register_post_type( 'post', array(
        'labels' => array(
            'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
        ),
        'public'  => true,
        '_builtin' => false, 
        '_edit_link' => 'post.php?post=%d', 
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => 'post' ),
        'query_var' => false,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    ) );
}

This is not tested, but you can give it a try. It's better than changing the URL settings on htaccess - because it will apply to all posts and pages.

  • Thanks but I don't want to apply to all posts. Just the ones after a specific date. – Philip Jun 14 '15 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.