2
votes

I have a wordpress blog with the following URL structure:

www.mysite.com/2016/09/the-name-of-the-post/

However i noticed that for SEO Reasons it would be better to have a structure like:

www.mysite.com/blog/the-name-of-the-post/

I have around 1000 posts with the "old" url structure. I can change the url structure in wordpress. However i would need a 301 redirect if someone tries to access a post using the old url.

E.g.

person/ google bot tries to access page via /2016/09/the-name

-> 301 Redirect to /blog/the-name

What is the best practice to do so? Do I add 1000 lines to the .htaccess file and do redirects? Would that negatively influence the server response time cause apache has to check through a long list of redirects first?

Can I tweak a file in the wordpress file structe which checks, if the requested page is included in e.g. an array, it will redirect it to the new url?

Thank you very much for your suggestions

4
Related: Reference: mod_rewrite, URL rewriting and “pretty links” explained. This might even be a duplicate - though I'm not sure if wordpress has some built in way of handling this.HPierce

4 Answers

2
votes

You can simply change the URL structure in WordPress to the one you want to have. You don’t have to modify your .htaccess or anything else.

WordPress will understand which post is referenced by the URL and redirect to the new canonical URL automatically.

Just go to the Permalinks admin page and select the “Post name” option as described in the Codex.

However, if you’re introducing a prefix like /blog/, too, you must redirect to the new URL base through .htaccess, e.g.

RedirectMatch 301 ^/[0-9]{4}/[0-9]{2)/(.+)$ http://example.com/blog/$1 // taken from stackoverflow.com/a/42211746/
1
votes

You can use the following Redirect

RedirectMatch 301 ^/[0-9]{4}/[0-9]{2)/(.+)$ http://example.com/blog/$1

This will redirect all urls from this form /1234/12/foobar to this /blog/foobar

1
votes

Change the permalink to the new structure and add this in your .htaccess

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)/$ http://example.com/blog/$3
0
votes

I had the same problem before but I resolve this issue using simple 301 redirects plugin. https://wordpress.org/plugins/simple-301-redirects/ You can bulk upload 301 redirects using bulk upload add-ons of this plugin. Hope this will help you.