Currently, there are several templates that are set up with the {if no_entries} conditional throughout our site, but if someone types in the wrong third segment, it’ll show an empty template for that page.
Our templates need more of a conditional like this…
{if segment_3 != [any of that channel's url_titles]}
{redirect="404"}
{/if}
or even better, if it can look for the closest url_title to redirect to that page, but alas that is probably newer technology ( I heard Facebook does that with their users’ passwords )
Forums and docs just bring me back to the if no_entries conditional. Can anyone help me with what I’m looking for? Is there anything set up for this?
Sounds like what you are looking for is the require_entry="yes"
parameter https://docs.expressionengine.com/latest/channels/entries.html#require_entry. Then use the {if no_results}
conditional to redirect to 404.
From the docs:
This parameter tells the channel tag that it should expect the URL to contain a valid entry ID or a valid URL title. If an ID is not found in the URL, the tag will not display any data. Normally, the channel tag will show something, even if the URL doesn’t point to a particular entry. For example, your main channel page will typically show several of your most recent entries. Whereas your “single entry” pages, like your comment page, will show a single entry based on information in the URL. However, if one of your single entry pages is requested, but it doesn’t contain a valid ID, this parameter will tell the tag that you do not wish the template to display anything.
Now, regarding matching the closest URL. You technically could do that with PHP if you want to be adventurous or have someone develop that for you. Essentially, you would write a PHP function that would do some sort of fuzzy matching to return the entry_id
or url_title
of the closest matching entry.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.