.htaccess – Regex htaccess: find a string where the same substring repeats twice
Summary: I want to create an htaccess redirect that redirects release notes links to the current version – but the link to the current version itself hits thesame filter, which can result in an endless loop, so I want to catch this special case… and can’t.
So, I have release notes per version, with URLs like this:
https://docs.example.com/product/1.12/product-release-notes-1-12.html
https://docs.example.com/product/1.13/product-release-notes-1-13.html
What I want is to use htaccess to redirect links from a wrong version to the right one, so that https://docs.example.com/product/1.12/product-release-notes-1-13.html
becomes https://docs.example.com/product/1.12/product-release-notes-1-12.html
.
The redirect itself is simple:
RewriteRule ^product/1\.([0-9]+)/product-release-notes?(.*).html$ product/1\.$1/product-release-notes-1-$1.html [L,R=302]
However, it also hits the correct link, resulting in an endless loop.
I can fix it by adding a “stop processing” line per version before this line, such as:
RewriteRule ^product/1\.12/product-release-notes-1-12.html$ product/1\.$1/product-release-notes-1-12.html - [L]
However, that does require adding a new line for every new version. To avoid this, Id want a regex in this line that would catch https://docs.example.com/product/1.12/product-release-notes-1-12.html
and https://docs.example.com/product/1.13/product-release-notes-1-13.html
but not catch https://docs.example.com/product/1.13/product-release-notes-1-12.html
.
Is it possible to craft such a regex and if so, how? Thanks!
Read more here: Source link