nginx: rewrite regex to match URL without digit at the end
nginx: rewrite regex to match URL without digit at the end
I'm trying to set up a rewrite rule to accomplish the following:
/base/first
-> /base/search/first
/base/first
/base/search/first
/base/first/second
-> /base/search/first/second
/base/first/second
/base/search/first/second
And of course /base/search/first
shouldn't redirect to /base/search/search/first
/base/search/first
/base/search/search/first
first
and second
are placeholders for strings with alphabetical characters and dashes (no digits).
first
second
However, I don't want to rewrite URLs like the following:
/base/first/second/third/123
123
is a placeholder for a three-character digits-only string
123
I've tried the following location block and I can get the first two requirements, but it's also capturing URLs that end in 123
123
location ~* ^/base/(?!search)[^d]+$
rewrite ^/base/(?!search)(.+) https://$server_name/base/search/$1 permanent;
[^d]+
I'm trying to use the
[^d]+$
so that it doesn't match URIs that end in numeric digits. And... wow you're right. My browser was using the cached redirect from earlier attempts. This works!! Thank you. 🙏– aman
Aug 31 at 17:41
[^d]+$
Minor improvement:
rewrite ^/base/(?!search/)(.+)
otherwise your regex won't work for /base/searching/abc
– anubhava
Aug 31 at 17:54
rewrite ^/base/(?!search/)(.+)
/base/searching/abc
Thank you, that's a good point.
– aman
Aug 31 at 18:00
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is purpose of
[^d]+
? Did you try testing from a new browser?– anubhava
Aug 31 at 17:31