ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Serve 404 for invalid category in URL

November 11, 2010 2:40am

Subscribe [3]
  • #1 / Nov 11, 2010 2:40am

    xtalx

    2 posts

    I’ve been searching through the forum, user guide and elsewhere but haven’t found a solution. I would like to serve a 404 if an invalid category is present in a requested URL.

    sitename.com/templateGroup/category/categoryName/url_title

    This was asked some time ago in this thread. A solution was offered

    ...
    
    {if:elseif segment_2 =="category"}
    
            <section id="books" class="category results">
            {exp:channel:entries channel="books" limit="10"}
            
                    {categories}
                        {if category_url_title != "{segment_3}"}
                            {redirect="404"}
                        {/if}
                    {/categories}
                    
                    {book_entry}
            
            {/exp:channel:entries}
    ...

    another user, Boyink!, pointed out in this thread, that it won’t work if the first entry belongs to more than one category.

    Is there a way to do this with built in EE or otherwise?

    I’m sure there is a simple way to do it with php but I’m not sure how to execute it.

    Custom query to pull all the categories for the channel or category group
    Place them into an array
    Conditional that states segment_3 must equal at least 1 item in the array.
      If yes display the entries
      If not redirect to 404

    Any pointers or complete answers 😊 would be appreciated.

  • #2 / Nov 11, 2010 4:12pm

    Ingmar

    29245 posts

    Are you using strict URLs? You might also want to add require_entry=“yes” to your tag and use a tag like

    {if no_results}
    {redirect="404"}
    {/if}

    in addition.

  • #3 / Nov 11, 2010 7:56pm

    xtalx

    2 posts

    Yes I’m using strict URLs.

    The problem is that the following always redirects to 404 if the first entry has more than one category assigned to it.

    {categories}
        {if category_url_title != "{segment_3}"}
            {redirect="404"}
        {/if}
    {/categories}

    Adding require_entry=“yes” to the channel entries tag with the no_results conditional makes it 404 whether multiple categories are assigned to the first entry or not.

    Adding require_entry=“yes” to the channel entries tag without the no_results conditional doesn’t show any results with or without the first conditional(checking segment_3)

    Let me know if there is anything else you need to troubleshoot or anything I might try.

    Thanks

  • #4 / Nov 12, 2010 3:46am

    John Henry Donovan

    12339 posts

    xtalx,

    I tried a few different methods but came up with nothing for you I’m afraid. I can move this thread to the CodeShare Corner if you wish for some community ideas?

    Maybe also delve further in Low Seg2Cat. It might be leveraged to good use here

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases