htaccess - Redirect after Permalink change - What regex do I use?

admin2025-01-07  4

I'm completely out of my league with regex stuff. I've seen a lot of examples for putting redirect code into the .htaccess file, but the default examples are mostly for changing away from dates. Everything else says to use the Redirection plugin, which I already use for simple 1 off redirects, but I don't know the regex for a permalink change. We have 743 posts, so I'm not doing those 1-by-1!

Here's the old and new structure:

old: /%post_id%/%postname%
new: /%category%/%postname%

Any help with what I need to put into the .htaccess file or the redirection plugin for this specific permalink change, would be most appreciated!

I'm completely out of my league with regex stuff. I've seen a lot of examples for putting redirect code into the .htaccess file, but the default examples are mostly for changing away from dates. Everything else says to use the Redirection plugin, which I already use for simple 1 off redirects, but I don't know the regex for a permalink change. We have 743 posts, so I'm not doing those 1-by-1!

Here's the old and new structure:

old: /%post_id%/%postname%
new: /%category%/%postname%

Any help with what I need to put into the .htaccess file or the redirection plugin for this specific permalink change, would be most appreciated!

Share Improve this question edited Jun 14, 2018 at 16:03 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Jun 14, 2018 at 13:57 useruser 212 bronze badges 2
  • 1 It's not possible to implement a redirect from /%post_id%/%postname% to /%category%/%postname% using .htaccess, simply because the %category% is unknown. You will need to perform this redirect in WordPress, since only WP knows what category the %postname% belongs to. – MrWhite Commented Jun 14, 2018 at 14:52
  • That's what I was afraid of. When you say to perform it within WordPress, what are you thinking? I've seen some plugins that offer what I think will work but I was hoping to avoid adding more plugins. – user Commented Jun 14, 2018 at 17:24
Add a comment  | 

2 Answers 2

Reset to default 0

You can do this by regex rewriting the legacy URL as a query. You can do that either on the post_id or the postname, in this case I think the postname is probably safer (avoids false matches on some other path that might start with numbers).

In custom functions, something like this:

add_rewrite_rule( '^[0-9]+/(.*)/?', 'index.php?&name=$matches[1]','top' );

Some similar regex would work in .htaccess or a redirection plugin.

Basically this is rewriting the URL as a query and passing it on to WordPress. WordPress uses this to find the post and then returns the correct permalink as defined in your structure. Essentially it's two redirects.

I wouldn't do that, unless the website was a very small one, with few pages/posts, not planning any growth. It can be a bit better for SEO in terms of link structure, but there are speed penalties.

This article explains it nicely, though author's recommendation of using year/date is far from optimal, both in terms of speed and SEO I think.

/%post_id%/%postname% structure is as good as it gets for WordPress. Small sites, with a small number of pages are an exception - there go for the most SEO friendly link structure - whether just post name, or category/post-name, tag/postname etc - depending on your tag/category structure and content "arrangement" (is "logical sitemap" a valid English term?).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736251987a10.html

最新回复(0)