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.

Strict URLs not working

April 30, 2011 10:46pm

Subscribe [6]
  • #1 / Apr 30, 2011 10:46pm

    Jack Tse

    67 posts

    Hi EE Peeps,


    I’m on the latest EE build and am not re-writing URLs yet.

    I’ve turned on strict URLS (and assigned a test 404 page) and read this: http://ellislab.com/blog/entry/better_404s_with_strict_urls/

    http://example.com/index.php/news/sprts/

    There is no Template called “sprts”. Currently EE will ignore the misspelling and display this instead:
    http://example.com/index.php/news/index/

    When Strict URLs are enabled EE will display the specified 404 page and send 404 headers in both these situations.

    - From EE Blog Article

    In my situation:

    Good:
    http://almania.tchmachines.com/~bdjluskx/index.php/about/jeff-thompson

    Bad:
    http://almania.tchmachines.com/~bdjluskx/index.php/about/jeff-thompMISTAKEson

    It defaults back to my index page instead of sending a 404. There are no sub templates being used in the about section that handle “whole” pages.


    Can somebody help me out? I’ve noticed this on some of my other EE websites as well. Not sure if I am doing something wrong.

    THANKS!

  • #2 / May 01, 2011 2:53pm

    Boyink!

    5011 posts

    Strict URLs only validates segment 1 - so its ensuring that you have an /about/ template group.

    Usually what you do then is in your about/index template add:

    {if segment_2 !=""}
        {redirect="404"}
    {/if}

    The logic here is that EE will look for a /about/jeff-thompson/ template first.  If it finds one it will load it.  If not, EE will assume you meant the /about/ template with a URL title of /jeff-thompson/.  Then the code above kicks in to trigger the 404.

    The caveat here is if you are using the index template to display single entries then there’s a different 404 approach to use. Is that the case?

  • #3 / May 02, 2011 1:27pm

    Jack Tse

    67 posts

    Hi Mike,

    Yes, I am using the index template (in the usual way) to handle all requests inside that template group.

    I’m using this code to validate the second segment (as this section only goes two levels deep):

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

    Please share what that code looks like… Thanks!

    _____

    BTW - I highly recommend the Relationships Screen cast ($25) Train-ee/Mike did to understand how to use them for all the lurkers in the house. The EE User Guide is incredibly confusing.

    http://www.train-ee.com/courseware/screencasts/detail/relationships-in-expressionengine/

  • #4 / May 02, 2011 1:41pm

    Boyink!

    5011 posts

    Are the second segments entry url titles?

    If so then you’ll want to use the require entry parameter, along with a no results conditional.

    Require entry will cause the tag to return nothing if the url title isn’t valid - then you code a 404 redirect inside the if no results conditional to trigger 404 behavior :

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

    You’ll want to leave the segment_3 checking as well just to ensure nothing gets added past segment 2.


    Edit to add - and thanks for the testimonial!

  • #5 / May 02, 2011 2:05pm

    Jack Tse

    67 posts

    Ahhh… Lightbulb moment!

    So just to be clear, at the top of my page I’m running a page “validator” that has no purpose other than to validate the pages like this:

    {exp:weblog:entries weblog="product" limit="1" dynamic="on" require_entry="yes" disable="categories|member_data|pagination|trackbacks"}
    {if no_results}
    {redirect="404"}
    {/if}
    {/exp:weblog:entries}


    Also - I noticed this method is breaking my normal index page conditional that controls the single segment overview page:

    {exp:weblog:entries weblog="product" limit="1" dynamic="on" {if segment_2==""}entry_id="24"{/if}require_entry="yes" disable="categories|member_data|pagination|trackbacks"}
    {if no_results}
    {redirect="404"}
    {/if}
    {/exp:weblog:entries}

    Should I be handling this in another way?

  • #6 / May 02, 2011 2:52pm

    Boyink!

    5011 posts

    Can you post the complete template code?

  • #7 / May 02, 2011 5:20pm

    Jack Tse

    67 posts

    I guess I was envisioning the way I normally use 404s at the top of my template but it could be used between the normal EE tags. This breaks my default segment_1 page. I want that page to be entry_id=“24”.

    Simplified Template:

    <html>
    <head>
    {exp:weblog:entries weblog="widget" limit="1" dynamic="on" {if segment_2==""}entry_id="24"{/if} require_entry="yes" disable="categories|member_data|pagination|trackbacks"}
    
    {if no_results}
    {redirect="404"}
    {/if}
    
    <title>{title_tag}</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="{meta_description}" />
    <meta name="keywords" content="{meta_keywords}" />
    </head>
    <body>
    
            <!--Section Header Begins-->
            <div id="section_header">
    
                {headline_image}
    
                <h1>{headline}</h1>
                {subtext}
            
            </div>   
            <!--Section Header Ends-->
    
            <!--Content Home -->
            <div id="content"> 
                    {content}           
    {/exp:weblog:entries}              
    
            <!--Content Home Ends-->
            </div>
    
    </body>
    </html>
  • #8 / May 02, 2011 6:01pm

    Sue Crocker

    26054 posts

    Thanks for the assist, Mike

    Jack, if you do your if statement higher up in your code..

    {if segment_2==""}
    your code goes here
    {if:else}
    your code goes here
    {/if}

    will that help with organization?

  • #9 / May 02, 2011 10:48pm

    Jack Tse

    67 posts

    Problem:
    For whatever reason, my default overview page stops working:

    Broken:
    example.com/widget

    Not Broken:
    example.com/widget/thing

    I tried simplifying it as much as possible to tease out the issue. This code is in NOT in the index, it’s inside a template inside a template group:

    {if segment_2 ==""}
    
    {exp:weblog:entries weblog="widget" limit="1" dynamic="off" entry_id="24" disable="categories|member_data|pagination|trackbacks"}
    {custom_field goes here}
    {/exp:weblog:entries}
    
    {if:else}
    
    {exp:weblog:entries weblog="widget" limit="1" dynamic="on" require_entry="yes" disable="categories|member_data|pagination|trackbacks"}
    {if no_results}
      {redirect="404"}
    {/if}
    {custom_field goes here}
    {/exp:weblog:entries}
    
    {/if}

    The above code works fine but as soon as I add the if no_results conditional, it breaks my single segment default page!

    ___

    So I got it to work by applying the conditional on the index where I chunk out the template. This code IS in the index:

    {if segment_2 ==""}
    {embed="services/html_head"}
    {if:else}
    {embed="services/html_head_1"}
    {/if}

    Can somebody please tell me why one would work and not the other? Is it important that it has to be in the index?


    Thank you all, this is very confusing.

  • #10 / May 03, 2011 7:00pm

    Sue Crocker

    26054 posts

    You really don’t need the if_no_results if you do it the way you’re doing it.

    Or are you doing the if_no_results inside of the second embed?

  • #11 / May 03, 2011 7:41pm

    Jack Tse

    67 posts

    Sue - they are inside the embed.

    I’m going to do an isolated test to try to get it on one template. Wish me luck. Will report back…

  • #12 / May 03, 2011 9:47pm

    Jack Tse

    67 posts

    My testing has revealed that the redirect variable is causing the issue. Can someone tell me why the redirect tag would do this?

    It’s the second {redirect="404"} that is causing me issues. I left the first part to show you the full page as it is in the live examples.


    This works as expected:

    http://almania.tchmachines.com/~bdjluskx/index.php/NO_REDIRECT_TAG/

    http://almania.tchmachines.com/~bdjluskx/index.php/NO_REDIRECT_TAG/10-reasons-why-cold-calling-is-a-total-waste

    {if segment_3 !=""}
        {redirect="404"}
    {/if}
    
    <html>
    <head>
    <title>Index</title>
    </head>
    
    <body>
    
    {if segment_2 == ""}
    
    {exp:channel:entries channel="articles" limit="1" dynamic="off" require_entry="no" entry_id="8" disable="categories|member_data|pagination|trackbacks"}
    <h1>{article_headline}</h1>
    {/exp:channel:entries}
    
    {if:else}
    
    {exp:channel:entries channel="articles" limit="1" dynamic="on" require_entry="yes" disable="categories|member_data|pagination|trackbacks"}
    {if no_results}
    NO REDIRECT TAG - JUST TEXT
    {/if}
    <h1>{article_headline}</h1>
    {/exp:channel:entries}
    
    {/if}
    
    
    </body>
    </html>

     

    This does not work:

    http://almania.tchmachines.com/~bdjluskx/index.php/REDIRECT_TAG/

    http://almania.tchmachines.com/~bdjluskx/index.php/REDIRECT_TAG/10-reasons-why-cold-calling-is-a-total-waste

    {if segment_3 !=""}
        {redirect="404"}
    {/if}
    
    <html>
    <head>
    <title>Index</title>
    </head>
    
    <body>
    
    {if segment_2 == ""}
    
    {exp:channel:entries channel="articles" limit="1" dynamic="off" require_entry="no" entry_id="8" disable="categories|member_data|pagination|trackbacks"}
    <h1>{article_headline}</h1>
    {/exp:channel:entries}
    
    {if:else}
    
    {exp:channel:entries channel="articles" limit="1" dynamic="on" require_entry="yes" disable="categories|member_data|pagination|trackbacks"}
    {if no_results}
    {redirect="404"}
    {/if}
    <h1>{article_headline}</h1>
    {/exp:channel:entries}
    
    {/if}
    
    
    </body>
    </html>

    Thanks, fine EE people!

  • #13 / May 04, 2011 2:44pm

    Sue Crocker

    26054 posts

    Hi Jack. I’m going to try to work on something locally. But before I do, can you post again exactly what you want to have happen, and some sample URLs.

  • #14 / May 04, 2011 3:48pm

    Jack Tse

    67 posts

    ExpressionEngine v2.1.3 - Build: 20110411

    My issue is with non-existent URLS being sent to 404 pages.

    I want the existing URLS to work:
    http://almania.tchmachines.com/~bdjluskx/index.php/NO_REDIRECT_TAG/
    http://almania.tchmachines.com/~bdjluskx/index.php/NO_REDIRECT_TAG/10-reasons-why-cold-calling-is-a-total-waste

    and NON-existing URLS to be sent to a 404 page:
    http://almania.tchmachines.com/~bdjluskx/index.php/NO_REDIRECT_TAG-FAKE/
    http://almania.tchmachines.com/~bdjluskx/index.php/NO_REDIRECT_TAG/10-reasons-why-cold-calling-is-a-total-waste-FAKE

    I also need it to work with conditionals in the same template. (I got it to work using conditionals on my index page when applied to embed but that method is not practical for a large website. Making it work in the above code would be ideal).

    I’m so close but that redirect variable makes the first conditional (that controls the default overview page) not work. It’s what I detailed in the last post.

    Thank you so much for the assistance!

  • #15 / May 05, 2011 5:51pm

    Kevin Smith

    4784 posts

    Hey Jack,

    This looks like it might be a parse order issue. Advanced conditionals are processed later than simple conditionals, so let’s try that.

    Right now your {if segment_2 == ""} conditional is considered “advanced” because it contains an {if:else}. Try breaking it up instead so it looks like this:

    {if segment_3 !=""}
        {redirect="404"}
    {/if}
    
    <html>
    <head>
    <title>Index</title>
    </head>
    
    <body>
    
    {if segment_2 == ""}
    
    {exp:channel:entries channel="articles" limit="1" dynamic="off" require_entry="no" entry_id="8" disable="categories|member_data|pagination|trackbacks"}
    <h1>{article_headline}</h1>
    {/exp:channel:entries}
    
    {/if}
    
    {if segment_2 != ""}
    
    {exp:channel:entries channel="articles" limit="1" dynamic="on" require_entry="yes" disable="categories|member_data|pagination|trackbacks"}
    {if no_results}
    {redirect="404"}
    {/if}
    <h1>{article_headline}</h1>
    {/exp:channel:entries}
    
    {/if}
    
    
    </body>
    </html>

    That should move it up enough in the parse order to fix it. Let us know what happens!

    Kevin

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

ExpressionEngine News!

#eecms, #events, #releases