2

My site's index page template uses switchee to determine whether to serve up index (home) page or a "single page" entry from the static channel.

I'm attempting to set up a 404 page for the site, and created a 404 template in the default site template group.

Once I set up 404s, all the "static" entries display as 404 instead.

I'd like to keep the option of single pages at the segment_1 level, so the site can add other single-purpose landing pages over time.

My Global Template Preferences:

  • Enable Strict URLs Yes, No, (this setting makes no difference)
  • 404 Page: site/404 (when set to None, things display okay)

Index template contains the following:

{exp:switchee variable="{segment_1}" parse="inward"}

{!-- ----- HOME PAGE ----- --}
{case value="index|''"}
       ...
{/case}

{!-- ----- Static Pages + Sidebar Asides ------ --}
    {case default="Yes"} 

     {exp:channel:entries 
        channel="static" 
        limit="1" 
        dynamic="no" 
        disable="categories" 
        require_entry="yes" 
        url_title="{segment_1}"
        }

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

        ... 
   {/exp:channel:entries}
    {/case}

{/exp:switchee}

EDIT NOTE (and apology): When I first posted this, I inadvertantly pasted the {if switchee_no_results} outside the {exp:channel:entries} tag pair. But it was inside that tag pair in my actual code. So sorry to have misled you. It was not my intention.

(the if switchee_no_results, require_entry, url_title="{segment_1}" were all recently added while trying to get things working with 404s)

Here is Template Debugging for the about page (an entry in the static channel)

When Global Template Preferences are set to strict=No and 404 Page=None:

(0.000006 / 4.56MB) - Begin Template Processing -
(0.000940 / 4.60MB) URI: about
(0.000952 / 4.60MB) Path.php Template: /
(0.000963 / 4.60MB) Retrieving Template
(0.000972 / 4.60MB) Parsing Template URI
(0.005528 / 4.80MB) Showing index. Template not found: about
(0.005556 / 4.81MB) Retrieving Template from Database: site/index
(0.006652 / 4.81MB) Template Found
(0.007026 / 4.82MB) Retrieving Template from File

When Global Template Preferences changed to 404 Page=site/404

(0.000005 / 4.56MB) - Begin Template Processing -
(0.000966 / 4.60MB) URI: about
(0.000978 / 4.60MB) Path.php Template: /
(0.000989 / 4.60MB) Retrieving Template
(0.000998 / 4.60MB) Parsing Template URI
(0.005462 / 4.80MB) Template group and template not found, showing 404 page

Other things I have tried (which have not made any difference):

{case value="{segment_1}" default="yes"}

Based on James Smith's URL schematic which he mentions here on EESE (topic: strict urls), I fear I broke some cardinal rule about segment_1. I hope not.

Are you aware of any way I can keep the basic setup as described (a single, index template uses switchee to serve up both home page and single entries in the static channel) and make the 404s work right?

UPDATED: I took this conversation to the switchee support forum on devotee, and based on Mark Croxton's response, I implemented a fix and describe it below.

2 Answers 2

1

The answer is: Don't use switchee on the site/index template if using a 404 template.

My solution, Long Story Short:

  1. Use Pages for all segment-1-style one-off static pages.
  2. Break up the original template into two templates.

The kernel of the answer was in my statement:

Based on James Smith's URL schematic which he mentions here on EESE (topic: strict urls), I fear I broke some cardinal rule about segment_1. I hope not.

Well, I did break the cardinal rule.

I took up the matter in the devotee switchee forum, and after I provided more template debugging reports, Mark Croxton referred to the James Smith schematic, and said:

[Mark Croxton:] To use static pages in segment_1 requires the Pages or Structure modules. Alternatively you could use an htaccess rule or one if the new routing add-ons to create custom routes for static pages.


Gory Fix Details, or, How I implemented Pages
(I know, I know. I have not used Pages much.)

I removed switchee entirely from this site/index template. Breakdown of what I did with what was in each case statement.

{!-- ----- HOME PAGE ----- --}
{case value="index|''"}
   ...
{/case}

Everything inside case value="index|&c" remained in the site/index template. (I'm showing the case statements here to refer back to the other template code posted in the original Q and other replies)

I created a second template, site/static. Everything inside the case (below) went there.

{!-- ----- Static Pages + Sidebar Asides ------ --}
{case default="Yes"} 
{exp:channel:entries channel="static"  ..etc... }
... 
{/exp:channel:entries}
{/case}

In Pages Module configuration, I set the Default Template for the Static channel to site/static. Then I went and changed all the entries in the static channel to Pages, and gave them their custom URLs.

Fixed.

0

I might try the following:

{exp:switchee variable="{segment_1}" parse="inward"}

{!-- ----- HOME PAGE ----- --}
{case value="index|"}
   ...
{/case}

{!-- ----- Static Pages + Sidebar Asides ------ --}
{case default="Yes"} 

 {exp:channel:entries 
    channel="static" 
    limit="1" 
    dynamic="no" 
    disable="categories" 
    require_entry="yes" 
    url_title="{segment_1}"
    }

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

    ... 

 {/exp:channel:entries}

{/case}

{/exp:switchee}

Subtle differences - note that the first switch case looks for a value of "index|" instead of index|'' and the if no results is placed within the entries loop in the default case (I'm not aware of an if switch_no_results, though it may exist - but in reality, setting a default case with no value to match is in effect the if else conditional).

4
  • Apologies for misleading you on the {if switchee_no_results} -- it was already inside the tag pair. My code sample in the Question, above, was wrong. (bad copy and paste of code from template to EESE). I tried your index| change, but that did not work.
    – auntialias
    Aug 9, 2013 at 19:26
  • You're viewing it at the root, right - with no segment 1? Do you get your homepage output that way? Aug 9, 2013 at 23:07
  • Home page appears just fine.
    – auntialias
    Aug 10, 2013 at 16:55
  • BTW, I am working with Mark Croxton on this at devot-ee.com forum for switchee. Will keep all posted here on what happens.
    – auntialias
    Aug 10, 2013 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.