1

I am trying to write an nginx rewrite directive that redirects a URL like https://website.com/2018/11/20/dummytext/feed/ to https://website.com/2018/11/20/dummytext/ (removing the /feed/ in the end).

I cannot just do a match for (.*)/feed/ because I do not want to redirect https://website.com/feed/.

I've created this regex that matches all posts with /feed/ in the end, which appears to work: https://www.regextester.com/?fam=111507

This is what I have in nginx:

rewrite "^\b(19|[2-9][0-9])\d{2}\b\/(.*)\/feed\/?$" https://website.com/$1 permanent;

The first problem is that it does not appear to work (I have restarted nginx). When I visit a URL like https://website.com/2018/11/20/dummytext/feed/ I am not redirected anywhere. The second problem is that I'm not sure about this part https://website.com/$1 and whether it would actually cut the /feed/ from the URL keeping the rest of the URL as is.

1

Not sure if Nginx supports \b, but you only need to use / (without a backslash).

You do not need to specify https://example.com in the replacement URI if the value remains unchanged. See this document for details.

In the example in your question, two values are captured ($1 and $2), but only one is used in the replacement URI.

Try:

rewrite "^/(19|[2-9][0-9])\d{2}/(.*)/feed/?$" /$1/$2/ permanent;

You mention that .* also matches zero characters, however, .+ matches at least one character. Also, the placement of multiple / characters in the pattern can avoid the /feed edge condition.

For example:

rewrite ^(/.+/)feed/?$ $1 permanent;

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.