2

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

Tom
  • 906
  • 1
  • 8
  • 30
  • Related: [Reference: mod_rewrite, URL rewriting and “pretty links” explained](http://stackoverflow.com/questions/20563772/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 Feb 13 '17 at 18:51

4 Answers4

2

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/
lxg
  • 12,375
  • 12
  • 51
  • 73
  • If i do so, i get a 404 error when accessing the blog post using the old URL – Tom Feb 13 '17 at 19:57
  • Have you previously set up the common WordPress .htaccess snippet according to https://codex.wordpress.org/Using_Permalinks#Creating_and_editing_.28.htaccess.29 ? If not, then of course you must add it once. After this has been done, WP will usually redirect posts correctly. – lxg Feb 13 '17 at 20:09
  • @Tom: To get the idea, try on an existing WP blog. For example, the URL https://wordpress.org/news/wordpress-4-7-2-security-release/ is one that points to a post on wp.org, but with a “wrong” permalink structure. Notice how you are being redirected to https://wordpress.org/news/2017/01/wordpress-4-7-2-security-release/ . This is done by WordPress internally. – lxg Feb 13 '17 at 20:13
  • Of course, as [Christina pointed out](http://stackoverflow.com/a/42212088/3908235), you must avoid using the `/blog/` prefix twice. – lxg Feb 13 '17 at 20:15
  • yes the htaccess file is as described in the codex. – Tom Feb 13 '17 at 20:26
  • Your approach should also work for a redirect from example.com/2016/10/name to example.com/%category%/name right? (%category% being the dynamically pulled category of the blog post – Tom Feb 13 '17 at 20:28
  • It works for all valid variants of permalinks. “Valid” in this case means that the permalink is unambiguous and indicates a single post. This means that at least the post slug or ID must be present in the URL; everything else is optional. So, yes, the `/%category%/%postname%` variant should also work. – lxg Feb 13 '17 at 20:38
  • I thought this was the case too, but I've had my 301 plugin (which I just tested and doesn't work with 4.7.1 and up) installed and just forgot about it. If you change the permalink slug of the post, the 301 is automatic, but the permalink structure requires the htaccess. http://yoast.com/wordpress-seo-url-permalink/ – Christina Feb 13 '17 at 20:54
  • 1
    Ok, to summarize: If you just change your permalink from `/%year%/%month%/%postname%` to `/%category%/%postname%`, you don’t have to modify your .htaccess. However, if you’re introducing a prefix, too, you must *also* redirect via .htaccess. Sorry for the confusion, I’ve updated my answer. – lxg Feb 13 '17 at 21:03
1

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

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
1

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
Shuja Shabandri
  • 557
  • 4
  • 12
0

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.

Prajan Karmacharya
  • 373
  • 1
  • 4
  • 13