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

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

1 Answer 1

2

Try:

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

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

3
  • 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. Sep 8, 2016 at 11:10
  • You need to add it to your existing server block. See this document on server blocks. Sep 8, 2016 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 ! Sep 8, 2016 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .