Nginx rule to redirect wordpress blogs - dropping the date

Recently, we changed the Gogobot blog to drop the dates from the posts.

Since we have links coming in to the “old” structure, we of course wanted to redirect from the old structure to the new structure.

The “old” structure is the common structure for wordpress /blog/2016/01/31/some-post-title and we changed it to be just /blog/some-post-title.

The requirements are pretty simple here, simply drop the date and redirect to the title, keeping all utm params and anything else in the url.

Here’s how you do it:

rewrite "/blog/[\d]{4}/[\d]{2}/[\d]{2}/(.*)" "/blog/$1" permanent;

Obviously, for us, everything is done through chef, I never edit anything on the server itself, but you need to verify that the configuration is passing syntax checks using nginx -t built in command.

After applying the configuration and restarting nginx, all “old” structures will redirect to the new one.