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;





What is purpose of [^d]+? Did you try testing from a new browser?
– anubhava
Aug 31 at 17:31


[^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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)