2

As you know from previous posts, that I am new to EE and I thank you for your help.

Here is a little background:

You know the "URL Title" field and how that can produce part of the URL, I was told to change that.

Example:

Change www.example.com/products/generation3-product to www.example.com/products/g3-product

The way that I changed it was by changing the "URL Title" field from generation3-product to g3-product and it worked.

Great, right? No

Now that I have changed it - yes, you are able to go to www.example.com/products/g3-product and see the product that you expect, but now the old URL still exists - www.example.com/products/generation3-product - but it displays a product that has nothing to do with the previous entry.

Thanks in advance

  • Could cache be in issue? – Timo002 Jul 11 '14 at 22:46
1

With Justin pointing me in the right direction, I was able to figure out the PHP error for no object created (yet)

{exp:channel:entries channel="products" limit="1" require_entry="yes" url_title=" {segment_2}"}
{if no_results}
    <script type="text/javascript">location.href = "http://"+document.location.host+"/products/noresultsfound/b90b6368c787883a8c707cf36870162a/";</script>
{/if}

If no entries match, then javascript will redirect to the products no result template

| improve this answer | |
1

EE is going to try to figure out what content to serve automatically unless you specify what content to grab. So when you have a URL that has a valid URL title like www.example.com/products/g3-product EE will recognize that the last segment is a valid URL title and show that content. When you have a non-valid url_title like www.example.com/products/generation3-product EE is going to first try to find an entry with the url title of the last segment. If it doesn't it is going to show the first entry that matches your channel entries tag (in most cases this will be the most recent entry).

James Smith wrote a great URL Schematic chart of what and how EE processes URLs so take a look at that to help you get started.

There are a couple of things that you can do.

First you will want to add a few things to your template but without seeing your template now I am going to have to guess. You probably have something like:

{exp:channel:entries channel="products" limit="1"}
 {title}
 ... Other tags here ...
{/exp:channel:entries}

You can add the require_entry and url_title fields to have better results.

{exp:channel:entries channel="products" limit="1" require_entry="yes" url_title="{segment_2}"}
 {title}
 ... Other tags here ...
{/exp:channel:entries}

Even better you can add a redirect to the 404 page if the URL title is not valid.

{exp:channel:entries channel="products" limit="1" require_entry="yes" url_title="{segment_2}"}
  {if no_results}
    {redirect="products/noresultsfound"}
  {/if}

  {title}
  ... Other tags here ...

{/exp:channel:entries}

That should prevent EE from showing you the wrong entry. The next step would be to setup some 301 Redirects in either you .htaccess file or using an add-on such as Detour Pro to direct the user from the wrong URL to the correct URL.

.htaccess 
Redirect 301 products/generation3-product www.example.com/products/g3-product
| improve this answer | |
  • First, thank you for your help. The 'require_entry="yes" url_title="{segment_2}"' definitely work with an un-formatted page appearing, but when I try using {if no_results} {redirect="404"}, then I receive a php error - Message: Creating default object from empty value – kronus Jul 10 '14 at 15:31
  • OK, I have figured it out, with your help. I used javascript within the if statement to redirect - {if no_results} <script type="text/javascript">location.href = "http://"+document.location.host+"/products/noresultsfound/b90b6368c787883a8c707cf36870162a/";</script> {/if} - and it worked. Thank you – kronus Jul 10 '14 at 16:17
  • You should be able to replace {redirect="404"} to any template or template group you need, as I have done in my template code above. – Justin Long Jul 10 '14 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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