Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Uauh. I thought there would be an easy way to do that. We've got over 7.000 posts on the site, so making that list would be a tough task :( –  javipas Mar 23 '11 at 16:23
    
Not really. A SELECT to get the to-from list, plus a script or vi macro to add rewrite ..?$ .... permanent; to the lines. –  coredump Mar 23 '11 at 16:58
    
Could you be a little more specific? I'm not an expert with MySQL, and though I know how to make a SELECT, I'm not sure the kind of SELECT you're talking about. In fact, shouldn't I make two SELECTs? One before setting the date on the permalinks, and one after, to get all the URLs and configure that file? –  javipas Mar 24 '11 at 7:55
    
You have the post publishing dates already on the database, so you should be able to select them before changing the permalink scheme. Take a look here codex.wordpress.org/Database_Description –  coredump Mar 24 '11 at 14:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.