We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

If no_entries != if url_title != any entry url_title's

How Do I?

MScholl's avatar
MScholl
1 posts
4 years ago
MScholl's avatar MScholl

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?

       
Andy McCormick's avatar
Andy McCormick
183 posts
4 years ago
Andy McCormick's avatar Andy McCormick

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.