Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-07-25 14:38:42

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

Regex not working as expected

I’m trying to implement some clean date filter URLs but am not using the date-based url scheme in the Textpattern settings for the site. I’ve got the following in my .htaccess file:

    # Rewrites for clean /YYYY or /YYYY/MM urls for date filtering
    RewriteRule ^([12]\d{3})/?$ index.php?month=$1 [L]
    RewriteRule ^([12]\d{3})(/|-)(0[1-9]|1[0-2])/?$ index.php?month=$1-$3 [L]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(.+) - [PT,L]

    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^(.*) index.php

    RewriteCond %{HTTP:Authorization}  !^$
    RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]

The intention is to catch and invisibly rewrite the following urls:

domain.com/2017/11/   domain.com/index.php?month=2017-11
domain.com/2017-11/   domain.com/index.php?month=2017-11
domain.com/2017/11    domain.com/index.php?month=2017-11
domain.com/2017-11    domain.com/index.php?month=2017-11
domain.com/2017/      domain.com/index.php?month=2017
domain.com/2017       domain.com/index.php?month=2017

When I enter the messy url(s) into the browser title bar, Textpattern filters the output correctly.

When I paste the above htaccess infos into this htaccess tester I get the desired output for my various test urls but in my local testing environment Textpattern throws a 404.

What am I doing wrong?


TXP Builders – finely-crafted code, design and txp

Offline

#2 2019-07-25 21:22:52

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Regex not working as expected

I’m by no means an expert but how about you convert them to redirects?

I have a suspicion that Txp is ‘falling through’ and trying to process them with the standard URL handler, failing and issuing a 404 because it doesn’t understand the month format unless explicitly told about it (could have sworn Oleg did something about that recently, but it might have been in dev).

Anyway, does this work?

    RewriteRule ^([12]\d{3})/?$ /index.php?month=$1 [R,L]
    RewriteRule ^([12]\d{3})(/|-)(0[1-9]|1[0-2])/?$ /index.php?month=$1-$3 [R,L]

The only difference between that and yours being the leading / on the destination to force relative URLs and then the [R,L] directive at the end.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2019-07-26 09:28:37

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

Re: Regex not working as expected

Thanks Stef,

I was hoping to avoid the url itself being rewritten, but adding the R flag did reveal part of the problem. The url domain.com/2017/ rewrites to domain.com/path/to/web/root/index.php?month=2017. Adding RewriteBase / helped resolve that (though I’m pretty sure I never had to do that before) but unfortunately it still won’t work without the R flag to change the url in the browser.

Oh well, at least it works. Thanks again!

Bloke wrote #318861:

could have sworn Oleg did something about that recently, but it might have been in dev

He did in the dev but it’s only for the date-based url scheme and this site uses /title.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2019-07-26 10:09:50

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Regex not working as expected

jakob wrote #318866:

The url domain.com/2017/ rewrites to domain.com/path/to/web/root/index.php?month=2017.

Yeah, that’s what it did to me when I tried it, hence I tried the [R] flag and added the relative URL prefix. My go-to “fix” because I don’t fully understand how Apache does what it does! Didn’t think of using RewriteBase – good idea.

[dev changes are] only for the date-based url scheme and this site uses /title.

Ah, I thought we were considering making an assumption that anything datey-looking would get interpreted as a date – regardless of permlink scheme – if it didn’t find a matching real Section in the site. But maybe /title makes that difficult, I’m not entirely sure.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB