Twig Error Runtime When Using Conditionals I am trying to create a meta include to display all my meta data and I have the following set up so far:

3. {% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" %}
  4. &lt;title&gt;{% if entry.metaTitle is defined %}{{ entry.metaTitle }}{% else %}{{ entry.title }}{% endif %} | {{ siteName }}&lt;/title&gt;
  5. {% if entry.metaDescription is defined %}
  6. &lt;meta name="description" content="{{ entry.metaDescription }}"&gt;
  7. {% endif %}
8. {% endif %}

{# 404 =============== #}

{% if craft.request.getSegment(1)  == "404" %}
  &lt;title&gt;Page Missing | {{ siteName }}&lt;/title&gt;
  &lt;meta name="description" content="Sorry the page you are looking for is missing, it may have been moved or deleted, please use the navigation to search for similar content."&gt;
{% endif %}


If I visit main pages the code works and if I go to example.com/404 the code works, but if I visit an invalid url example.com/foo then I get a Twig Error Runtime error:
Craft doesn't redirect to 404 if a page isn't found, it just loads the 404 template and keeps the invalid uri.

What currently happens:


example.com/invalid_uri
Craft doesn't find it and shows the 404 template
Your conditional checks if 404 is in the uri, but it isn't.
Your code now tries to access the entry variable which isn't defined.
This results in a Twig error.


.

Solution:

Add an entry is defined to your conditional:

{% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" and entry is defined %}
....
{% endif %}