Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-02-23 15:19:39

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

404 pagination without articles

Hi all! How would I make page urls like ?pg=2 go to a 404 if they don’t have content on them? Esp. for blog clean ups where there might be less pages than there were due to deleted blogs, I’d like to 404 those pages if they don’t have any articles on them if possible?

Thanks so much!


…………………
I <3 txp
…………………

Offline

#2 2022-02-23 23:53:25

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

Re: 404 pagination without articles

Some late-night uncoordinated thoughts but maybe it helps you or someone else come up with something better.

Idea 1: I believe you can use txp:article(_custom) as a container tag and put an txp:else statement in it, e.g.

<txp:article … >
    <!-- your tags or txp:output_form -->
<txp:else />
    <!-- no results – show error or redirect to 404 page -->
</txp:article>

And perhaps Oleg’s trick in this post could be used to redirect to a 404 page.

Ideas 2: You can use txp:article(_custom) with the pgonly attribute to get the total number of articles in your current txp:article filter. Save that in a txp:variable, divide it by your limit and compare what that produces against your ?pg value from the url (which you can get with page_url or using the gps function in a <txp:php> block). If it exceeds the rounded up value of pgonly, return a 404 (or redirect to 404).


TXP Builders – finely-crafted code, design and txp

Offline

#3 2022-02-25 14:39:10

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

Re: 404 pagination without articles

Hey Jakob thanks so much for this, gonna test it out over the weekend, thanks!! 🙂


…………………
I <3 txp
…………………

Offline

#4 2022-02-26 22:41:01

hilaryaq
Plugin Author
Registered: 2006-08-20
Posts: 335
Website

Re: 404 pagination without articles

Figured it out, posting for anyone who comes across this issue!

So I use a form for single/static pages, but I needed a way to tell whether a page was a listing page or a single article page to control canonicals etc in the head, so in the default article form I already had

<txp:if_custom_field name="override_form" not value="page_single">
<txp:variable name="multi_page" value="1" />
</txp:if_custom_field>

The above in default form

<txp:hide process="2">
<txp:if_variable name="multi_page" value="1">
<txp:else />
<txp:txp_die status="404" />
</txp:if_variable>
</txp:hide>

The above in page head

So the logic is, is there an article on the page? (determined by multi_page variable), if there is, just do everything as normal, if a url is accessed without any article on it, return a 404, and this works to make any legacy pagination like ?pg=4 for example, if you now only have 3 pages, but perhaps that 4th page and even more are still indexed by search engines, the right way to treat them is as 404’s as they no longer exist.

Hope this helps someone else! A cute fix.


…………………
I <3 txp
…………………

Offline

#5 2022-02-27 00:54:27

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

Re: 404 pagination without articles

Nice! Does:

<txp:hide process="2">
<if::variable name="multi_page" not value="1">
   <txp:txp_die status="404" />
</if::variable>
</txp:hide>

work?


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

#6 2022-02-27 23:37:36

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

Re: 404 pagination without articles

Nice way of solving it. Thanks for posting it too. I totally forgot about txp:die – that’s also a much better way of producing a 404.

Does your method require you to specifically set an override form? or is that a trick for detecting the current form in use that I wasn’t aware of?
Otherwise, I guess you could also create a similar variable in the form you specify in listform and then use your method to check whether that variable exists.

One more question 🙄: can you (or someone) explain what the 2 in process="2" does and why you need it?


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB