1

I'm trying to setup a nginx rewrite rule but can't figure out how to get it working.

I want to redirect all urls https://domainname.com/archives/year/month/date/postname to https://domainname.com/postname

The page https://domainname.com/archives needs to stay active though.

Who can help me out?

Cheers, Jaap

Jaap de Wit
  • 88
  • 1
  • 7

1 Answers1

2

Try:

rewrite "^/archives/\d{4}/\d{2}/\d{2}/(.+)$" /$1 permanent;

See this document for details. A useful resource for regular expressions.

Richard Smith
  • 12,834
  • 2
  • 21
  • 29
  • Thanks the format is indeed yyyy/mm/dd. I've put this in a server block like this: server { rewrite "^/archives/\d{4}/\d{2}/\d{2}/(.+)$" /$1 permanent; } Restarted Nginx but the redirect is not working. – Jaap de Wit Sep 08 '16 at 11:10
  • You need to add it to your existing server block. See [this document](http://nginx.org/en/docs/http/server_names.html) on server blocks. – Richard Smith Sep 08 '16 at 11:21
  • Yes! First added it to the wrong one. Corrected this and now it's working except for a couple of urls. Will look into that. Thanks for your quick help @richard-smith ! – Jaap de Wit Sep 08 '16 at 11:47