Rewrite URLs with nginx

You might happen to have a WordPress blog whose URLs you need to rewrite, perhaps because you've changed category to topic and you don't want to lose that sweet search engine traffic.

Here's a quick way to do it with nginx:

  1. Open your site's configuration in your favorite text editor: sudo nano /etc/nginx/some.site
  2. Look for the the location block, it looks similar to this:
    location / {
         try_files $uri $uri/ =404;
    }
    
  3. Add the rewrite directive:
    location / {
         rewrite ^/category(.*)$ /topic$1 permanent;
         try_files $uri $uri/ =404;
    }
    

This redirects all /category requests to /topic. That's it!