<p>Craft doesn't redirect to 404 if a page isn't found, it just loads the 404 template and keeps the invalid uri.</p>

<p><strong>What currently happens:</strong></p>

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

<p>.</p>

<p><strong>Solution:</strong></p>

<p>Add an <code>entry is defined</code> to your conditional:</p>

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

CC BY-SA 3.0
2014-06-24T20:56:57.470Z
That did it Victor thank you, for info I also needed to add `or entry is not defined` to the last 404 conditional which makes sense with what you have explained.
Gareth
CC BY-SA 3.0
2014-06-24T20:51:41.657Z
2014-06-24T20:51:41.657Z
Victor
<p>Take a look at what you have in your <em>_includes/_page_meta</em> file at line 4.</p>

<p>Most likely you just need to wrap it in a conditional with <code>is defined</code> like</p>

<pre><code>{% if entry is defined %}
    {# your template stuff here #}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-06-24T20:52:52.430Z
I think Victor's answer is better!
Alex Kendrick
CC BY-SA 3.0
2014-06-24T20:58:32.380Z
Thanks Alex, yes Victor's solution fixed it (I added the line numbers in to try and help to).
Gareth
CC BY-SA 3.0
2014-06-24T20:50:22.257Z
2014-06-24T20:50:22.257Z
Alex Kendrick
<p>I am trying to create a meta include to display all my meta data and I have the following set up so far:</p>

<pre><code>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 %}
</code></pre>

<p>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:</p>

<p><img src="https://i.sstatic.net/cJmTb.png" alt="enter image description here"></p>

<templating><conditional>
Twig Error Runtime When Using Conditionals
# Twig Error Runtime When Using Conditionals

(Asked by: username_0 on 2014-06-24)

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. {% if entry.metaTitle is defined %}{{ entry.metaTitle }}{% else %}{{ entry.title }}{% endif %} | {{ siteName }}
  5. {% if entry.metaDescription is defined %}
  6. 
  7. {% endif %}
8. {% endif %}

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

{% if craft.request.getSegment(1)  == "404" %}
  Page Missing | {{ siteName }}
  
{% 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:

**Tags:** templating, conditional

---

## 2 answers

### Answer by username_1 on 2014-06-24. Score: 3 | Accepted answer

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 %}

> **Comments:**
> **username_0**: That did it Victor thank you, for info I also needed to add `or entry is not defined` to the last 404 conditional which makes sense with what you have explained.  

---

### Answer by username_2 on 2014-06-24. Score: 1

Take a look at what you have in your _includes/_page_meta file at line 4.
Most likely you just need to wrap it in a conditional with is defined like
{% if entry is defined %}
    {# your template stuff here #}
{% endif %}

> **Comments:**
> **username_2**: I think Victor's answer is better!  
> **username_0**: Thanks Alex, yes Victor's solution fixed it (I added the line numbers in to try and help to).  

---

