Nginx – Changing WordPress permalinks under Nginx: rewrite rule to avoid Google’s punishment

nginxrewriteWordpress

I have a site with a WordPress blog which has the usual permalink structure:

/%postname%/

But I'd like to include the date in the permalink:

/%year%/%monthnum%/%day%/%postname%/

So a post like this:

www.mysite.com/beautiful-title

would translate/redirect to this

www.mysite.com/2011/03/23/beautiful-title

I know I must apply a rewrite rule to send all previous permalinks (that are now indexed on search engines such as Google, Yahoo or Bing) to the new ones, because if I don't do so, all those links will get a 404, right?

What's the right way to do that in Nginx? That's the webserver I'm working with, but the structure of the rewrite rules is quite a mistery for me 🙁

********************* UPDATE ********************

Well, the question was finally solved thanks to the magic of WordPress. In fact, I haven't had to do anything with Nginx or rewrite rules.

The fact is, when Google shows a result of your WP blog, it goes to the link listed, but then WordPress converts automatically this link to the right one you've got according to your permalinks structure.

So I've just changed my permalinks structure that now has the date on the URL, and everything is working properly. No Google (or other search engines) punishment, after all 😉

Greetings!

Best Answer

You can use the information on Wordpress database and some SELECT's to assemble a from-to list of URLs that basically has the old URLS to the new URLS, like:

/beautiful-title /2011/03/23/beautiful-title

and then use this information to write a long, very long depending on the number of posts on your site, rewrite. It's not pretty. You would have a long file with:

location / {

    rewrite ^/beautiful-title/?$ /2011/03/23/beautiful-title permanent;
    rewrite ^/beautiful-title2/?$ /2011/03/22/beautiful-title2 permanent;
    ... and so on ...
}

Yes, you will have to put each of the old URLs pointing to the new ones, there's not much to do since the new URLs are depending on dates. The good news is that theoretically you must keep it up just for some time until the robots notice the permanent redirect (301) and update the database. The same can't be said about people who bookmarked your posts.

Related Topic