Nginx URL Redirect For Old WordPress Permalink

Table of Contents

Seems URL rewrite/redirect is a never ending story.

Background

When I switched my blogging platform from Ghost to Hugo, I also switched the web serving component.

I used my own h2ghost with Ghost as both were nodejs applications.

However Hugo produce static pages and I decided to look into other http server options.

I was debating between Monkey Server and Nginx. And finally pick Nginx as it is more actively developed.

Logwatch

Log was set to minimum when I was using h2ghost. But once I switched to Nginx, Logwatch start showing the 404 listing. To my surprise, other than all the random attacks, there are a few old WordPress permalink popping up.

/index.php/2012/12/06/tiny-vps-postfix/: 1 Time(s)
/index.php/category/phone/: 1 Time(s)
/index.php/sample-post/: 1 Time(s)
/index.php/tiny-vps-postfix/: 1 Time(s)

Redirect

To save those requests, I need to implement redirect/rewrite again, just like old times. Luckily the regex from my old Lighttpd still works.

# /index.php/YYYY/MM/DD/<article> to /blog/<article>
location ~ "^/index.php/\d{4}/\d{2}/\d{2}/(.*)$" { return 301 https://johnsiu.com/blog/$1; }
# /index.php/<article> to /blog/<article>
location ~ "^/index.php/(.*)$" { return 301 https://johnsiu.com/blog/$1; }

Now /index.php/tiny-vps-postfix/ works again!!

John Siu
Minimize the Effort, Maximize the Effect!
comments powered by Disqus