Changing WordPress permalink structure on Nginx

I decided to follow Joost de Valk’s (of Yoast fame) advice for ideal permalink structure and switch my permalinks from year/month/day/postname to postname.  Of course this means that any links already out there and any search engine indexes will now point to the wrong place.  Time for some redirects!

There are plenty of guides and tools available to help you if you decide to change your permalink structure whilst using Apache with its .htaccess files to host WordPress, however I use Nginx so I needed another approach.

The redirect is actually very simple; first detect that the URL is in the format using a regular expression.

The regular expression below checks that the url contains a slash followed by four numbers, followed by a slash, followed by two numbers, followed by another slash, followed by another 2 numbers, followed by yet another slash, followed by anything.  Which will match my previous permalink structure (i.e. www.ukitblog.co.uk/2013/07/22/deploying-airserver/‎)

 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$

And then, take the 4th (and final) part of the URL and append it to the sites URL

rewrite "^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$" http://www.ukitblog.co.uk/$4 permanent;

Giving a complete rule of:

location ~ "^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$" {
 rewrite "^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$" http://www.ukitblog.co.uk/$4 permanent;
}

It’s very easy to adjust the above snippet to apply to other styles of previous redirects, give me a shout in the comments if you need any help.

Also, if the change is only temporary you can use the word redirect instead of permanent to issue a 302 temporary redirect rather than a 301 permanent redirect.

The rule just needs to be inserted in your Nginx configuration for the specific site, probably located at /etc/nginx/sites-available/domainname.tld

Comments

5 responses to “Changing WordPress permalink structure on Nginx”

  1. Andre Bazaglia Avatar

    Hi Alex,

    Thank you for the post. I will change my permalink structure for the first time in 5 years. As you’ve done it in 2013, I’d like to hear from your experience: is adding this nginx rewrite all I need or is there anything else I should look at when I’m changing my permalink structure?

    Also, I can see you’re using rewrite instead of redirect. You may want to read this: http://serverfault.com/questions/407028/nginx-rewrite-or-return-for-redirection

    Thank you.

    1. alex@xiano.co.uk Avatar

      Hi Andre,
      Obviously you also need to change the permalink settings in WordPress also, but other than that the change to the nginx config is all that is needed.
      I didn’t have any problems with using rewrite instead of redirect, I’d be interested if someone could clarify to pros/cons of each method.

  2. ihsan Avatar

    Hi Alex,

    Thank you for your tuts. I have other problem because I want to chang my permalink to /postname/postid/. With your code just change ti /postname. Thank you

    1. alex@xiano.co.uk Avatar

      Hi Ihsan,
      Sorry in the delay getting back to you!
      I’m not sure you will be able to do what you want to do simply using rewrites as your original URL doesn’t include the postid.
      A plugin like “Redirection” by John Godley would do the trick, but if you have a vast number of posts that might not be an option.

  3. Ajay Avatar

    Thanks for this. The Yoast tool just gives me the .htaccess version and I’ve been hunting quite a bit for the nginx version since I moved from Apache to nginx

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.