Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2021-10-14 10:34:04

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,191
Website

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

#2 2021-10-14 10:46:48

etc
Developer
Registered: 2010-11-11
Posts: 4,549
Website GitHub

Re: htaccess regex query: matching against a previous group?

Hi Julian, I have updated your regex with \1 backref, please test. Not sure whether it works in .htaccess, but it should.

Offline

#3 2021-10-14 12:18:29

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,191
Website

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

#4 2021-10-15 06:37:49

etc
Developer
Registered: 2010-11-11
Posts: 4,549
Website GitHub

Re: htaccess regex query: matching against a previous group?

jakob wrote #331794:

If it does, that would be brilliant!

It looks working, at least.

Offline

Board footer

Powered by FluxBB