Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
htaccess regex query: matching against a previous group?
I have a situation where a legacy site has many sections where the landing page has been set up as an article with the same name as the section (or occasionally index), sometimes also with .htm or .html tagged on. I’ve simplified these to sticky articles so that one only needs a regular section permlink (e.g. <txp:site_url /><txp:section />/
) for the landing page.
I can catch these in txp with a custom url scheme handler but I was hoping to do this using .htaccess
so that I can send a 301 to search engines to indicate that the url has moved.
I have the following working except for the matching aspect:
^\/(russian|blog)\/(russian|blog|index)(.html?)?$ /$1/
Test urls by way of example (here with just two sections):
/russian/
/russian/russian
/russian/russian.htm
/russian/russian.html
/russian/index.htm
/russian/index
/blog/
/blog/blog
/blog/blog.htm
/blog/blog.html
/blog/index.htm
/blog/index
gives me /russian/
or /blog/
as output. That’s already a good start.
You can try it out on: regex101.com/r/TG0bye/1
This will, however, also match urls like /russian/blog
or /blog/russian
. As I have about 20 sections in reality resulting in more cross-permutations, so is there a way to match the previously matched group either using a match expression in regex or a two stage RewriteCond
pattern?
TXP Builders – finely-crafted code, design and txp
Offline
Offline
Re: htaccess regex query: matching against a previous group?
etc wrote #331792:
Hi Julian, I have updated your regex with
\1
backref, please test. Not sure whether it works in.htaccess
, but it should.
If it does, that would be brilliant! And the solution is so simple! I promise I did look in advance but started drowning in stackoverflow differences of opinion. Will try online as my local webserver is not apache-based.
Thank you Oleg!
TXP Builders – finely-crafted code, design and txp
Offline
Offline