I want to add categories to my URL structure. I'm sure this has been covered and I found many resources online to create the redirect code in .htaccess, but when I enter the code it doesn't work. Running an Apache server.
Essentially, I want to go from
to
I added the following to my .htaccess:
RedirectMatch 301 ^/([^/]+)/(\d+)/$ /$1
But I still get 404s when accessing links from outside sources (Google, Reddit etc)
I want to add categories to my URL structure. I'm sure this has been covered and I found many resources online to create the redirect code in .htaccess, but when I enter the code it doesn't work. Running an Apache server.
Essentially, I want to go from
https://example/this-is-a-post/1111
to
https://example/posts-category/this-is-a-post/1111
I added the following to my .htaccess:
RedirectMatch 301 ^/([^/]+)/(\d+)/$ https://example/$1
But I still get 404s when accessing links from outside sources (Google, Reddit etc)
Share Improve this question edited Oct 3, 2019 at 9:01 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Oct 3, 2019 at 2:03 cjmicjmi 212 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 2So first, I'm quoting @MrWhite's comment:
You can't do that sort of redirect in
.htaccess
- where are you expected to get theposts-category
part from?
And he's right — you shouldn't use the .htaccess
.
We have over 2,000 articles and 55,000 backlinks to these articles. If I just change the permalinks I will have over 50k 404s which will break the site.
You should just change the permalink structure and use custom code to redirect old URLs to the new ones.
Step 1: Change the permalink structure.
Just go to the Permalink Settings page and in the Custom Structure box, enter this:
/%category%/%postname%/%post_id%/
Step 2: Add custom PHP/WordPress code to redirect old URLs to new ones.
This is an example (tried and tested working) using the parse_request
hook: (this would go in your theme functions.php
file) (and the code uses these two WordPress functions: get_post_field()
and wp_redirect()
)
add_action( 'parse_request', 'maybe_redirect_old_permalinks' );
function maybe_redirect_old_permalinks( $wp ) {
if ( preg_match( '#^([^/]+)/(\d+)$#', $wp->request, $matches ) ) {
list ( $path, $slug, $id ) = $matches;
// Redirect to the new permalink, if the slug and ID match.
if ( $slug === get_post_field( 'post_name', $id ) ) {
wp_redirect( get_permalink( $id ), 301 );
exit;
}
}
}
This approach also means that both example/<post slug>/<post ID>
and example/<post category>/<post slug>/<post ID>
would work. But on the page (post content, widgets, etc.), post permalinks would only be in the later format.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745103757a4611434.html
/%category%/%postname%/%post_id%/
in the Custom Structure box. – Sally CJ Commented Oct 3, 2019 at 5:00.htaccess
- where are you expected to get theposts-category
part from? The above directive (apart from using the wrong directive with WordPress) would redirect to/this-is-a-post
- which does not seem to be what you require. But the redirect is not necessary to get this to work anyway, only to preserve SEO. – MrWhite Commented Oct 3, 2019 at 8:55