John Siu Blog

Tech - Business Tool, Personal Toys

Nginx Redirect For WordPress Permalink

☰ Table of Content

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.

1
2
3
4
# /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

Update: 2020-09-01
comments powered by Disqus