admin管理员组文章数量:1417406
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
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 Grossherr Commented Jun 14, 2015 at 17:06
4 Answers
Reset to default 6 +25Your 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.
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.
You might find the Redirection plugin interesting.
It should allow you to change the permalink structure while keeping old URLs valid.
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.
本文标签: Change permalink only on new posts
版权声明:本文标题:Change permalink only on new posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745259021a2650268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论