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

  • It should be a simple regular expression. Are year, month & date all numeric? Do you want a 301 or 302 redirect? – Richard Smith Sep 8 '16 at 10:08
  • Yes, all numeric and they should be 301. – Jaap de Wit Sep 8 '16 at 10:27
  • I have posted an answer which assumes the date format is yyyy/mm/dd – Richard Smith Sep 8 '16 at 10:30


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

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

  • 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 8 '16 at 11:10
  • You need to add it to your existing server block. See this document on server blocks. – Richard Smith Sep 8 '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 8 '16 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.