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.

{redirect} not working

August 18, 2007 2:34pm

Subscribe [2]
  • #1 / Aug 18, 2007 2:34pm

    Jeremiah C.

    8 posts

    I would like to redirect a template based on segments, such that example.com/myTemplateGroup and example.com/myTemplateGroup/myTemplateIndex are valid but example.com/myTemplateGroup/yoYoYo is invalid where myTemplateIndex in myTemplateGroup is the default template for EE.

    Example:

    {if (segment_1 == "" XOR segment_1 == "myTemplateGroup") AND (segment_2 == "" XOR segment_2 == "myTemplateIndex")}
    <!-- great -->
    {if:else}
        {redirect="404"}
    {/if}

    EE spits out {redirect="404"} instead of actually redirecting.

    (The reason for this is that I’m using the .htaccess method to remove index.php where every URI that does not match an actual file is sent to EE. Regardless of my reason, why does the {redirect} not work?)

    Much thanks in advance!

  • #2 / Aug 19, 2007 12:24am

    John Fuller

    779 posts

    That redirect tag is for the weblog module.  Is that what you are using it for?  I cannot tell from your snippet but I am guessing that you are not.

    Otherwise, you can take a look at this as another method for doing a redirect.

  • #3 / Aug 19, 2007 1:33am

    Jeremiah C.

    8 posts

    Thanks Manofsteel!

    The problem with the method linked is that it doesn’t return a 404 status, which is what I’m trying to achieve with the redirect.

    If I add the weblog module in, so that it looks like:

    {exp:weblog:entries limit="1"}
        {redirect="404"}
    {/exp:weblog:entries}

    EE outputs {redirect="404"} instead of actually redirecting. This is peculiar behavior, no?

  • #4 / Aug 19, 2007 2:01am

    John Fuller

    779 posts

    Thanks for the info, I needed to know how you were trying to use it.

    Put the redirect portion in between the no_results conditional as shown in the example on the page that I linked to.  In fact, if ever in doubt, a good thing to try is to copy the example and paste it into a test template.

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

     

    Make sure you have your 404 template set at “Global Template Preferences” on the templates tab.

    Edit: In other words, I think you are using the tag incorrectly for what you are trying to accomplish.

  • #5 / Aug 19, 2007 2:13am

    John Fuller

    779 posts

    You might take a look at this thread.  Looks like Arden was trying to use the tag incorrectly also but they eventually came up with a solution.

  • #6 / Aug 19, 2007 2:34am

    Jeremiah C.

    8 posts

    Thanks so much. It’s just not seeming to work.

    The sample works, however, when combined with the other conditions needing to be met, it doesn’t work. I tried both

    {if no_results AND ((segment_1 != "" XOR segment_1 != "home") OR (segment_2 != "" XOR segment_2 != "index"))}

    and

    {if no_results}
        {if ((segment_1 != "" XOR segment_1 != "home") OR (segment_2 != "" XOR segment_2 != "index"))}

    to no avail.

    I think I’m going to give up for now and not worry about any files not found in the /home/ directory.

    Thanks again for your help. It’s just a shame that EE pales in comparison to WordPress in URL customizability when it surpasses it in every other way. :-\

  • #7 / Aug 19, 2007 3:06am

    John Fuller

    779 posts

    Well I am not done yet.  😉 

    I apologize, I just wanted to show that you are using an apple tool to fix an orange problem.  The redirect feature you are looking at is for weblog entries rather than examining the URL and outputting a 404 based on that.  From your first snippet I was not 100% sure if I was seeing the whole story though.

    Do this…

    Create a template that you want to use as a 404 page.  Look at this thread.  The best posts probably start from that post down but you might want to read the whole thing.  You could use the redirect method I showed you to redirect to that page or you might see something better in that thread.  Then copy the code mentioned in that post I linked and paste it in your 404 template.  Remember that you have to enable PHP (for output) for the template when using that PHP block. 

    If you want to verify that you are getting a 404 header you can download the webdevloper add-on for Firefox and go information -> view response headers and it should show the 404.

  • #8 / Aug 19, 2007 11:16am

    Robin Sowell

    13255 posts

    Huh- I hadn’t seen that suggestion by Derek before.  Handy, that.  Good one, John.

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

ExpressionEngine News!

#eecms, #events, #releases