Answers.Body: <p>That is only logical. Your <code>tag</code> will be null so your query is actually 
  <code>{% set entries = craft.entries.relatedTo(null).order('title').limit(null) %}</code> and that results in 
  <code>{% set entries = craft.entries.order('title').limit(null) %}</code>
  So in the end you will fetch all entries. 
  This is what you need </p>
  
  <pre><code>{% set tag = craft.tags.slug(tag).first %}
  &lt;!-- check if the tag exists --&gt;
  {% if tag %}
      &lt;!-- the length filter actually fetches all entries -&gt; executes the query and caches all entries
      so you can just fetch them in the beginning. You'll gain no performance boost with that filter --&gt;
      {% set entries = craft.entries.relatedTo(tag).order('title').limit(null).find() %}
      {% if entries %}
          {% for entry in entries %}
              &lt;!-- code here you fetched entries --&gt;
          {% endfor %}
      {% else %}
          &lt;!-- you have a tag but no entries related to that tag --&gt;
      {% endif %}
  {% else %}
      &lt;!-- you have no tag -&gt; you won't find any entries --&gt;
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-13T20:41:19.747Z
Answers.LastActivityDate: 2017-11-13T20:41:19.747Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have 3 groups of tags that redirect to the same template. <code>tags/index.html</code>. All the routing and linking those tags to their group page works well.</p>
  
  <p><code>&lt;a href="{{ siteUrl }}taggroup/{{ tag.slug }}"</code></p>
  
  <p>The problem I have is that when I try to enter a tag that doesn't exist on the address bar (e.g., <a href="http://sitename/taggroup/unknowntag" rel="nofollow noreferrer">http://sitename/taggroup/unknowntag</a>) I get all entries displayed.</p>
  
  <p>Here's my <code>tags/index.html</code> code: </p>
  
  <pre><code>{% if not tag %}
      {% exit 404 %}
  {% endif %}
  
  {% set tag = craft.tags.slug(tag).first %}
  
  {% set entries = craft.entries.relatedTo(tag).order('title').limit(null) %}
  
   {% if entries | length %}
  
    {% for entry in entries %}
  
      &lt;!-- Code Here --&gt;
  
    {% endfor %}
  
    {% else %}
  
     &lt;!-- No Tag Code Here --&gt;
  
   {% endif %}
  </code></pre>
  
  <p>What Am I doing wrong? Thanks in advance. </p>
  
Tags: <templating><relations><tags>
Title: Search for non-existing tags shows all entries
ThreadText: # Search for non-existing tags shows all entries
  
  (Asked by: username_0 on 2017-11-13)
  
  I have 3 groups of tags that redirect to the same template. `tags/index.html`. All the routing and linking those tags to their group page works well.
  
  `<a href="{{ siteUrl }}taggroup/{{ tag.slug }}"`
  
  The problem I have is that when I try to enter a tag that doesn't exist on the address bar (e.g., <http://sitename/taggroup/unknowntag>) I get all entries displayed.
  
  Here's my `tags/index.html` code:
  
  ```
  {% if not tag %}
      {% exit 404 %}
  {% endif %}
  
  {% set tag = craft.tags.slug(tag).first %}
  
  {% set entries = craft.entries.relatedTo(tag).order('title').limit(null) %}
  
   {% if entries | length %}
  
    {% for entry in entries %}
  
      <!-- Code Here -->
  
    {% endfor %}
  
    {% else %}
  
     <!-- No Tag Code Here -->
  
   {% endif %}
  
  ```
  
  What Am I doing wrong? Thanks in advance.
  
  **Tags:** templating, relations, tags
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-11-13. Score: 2 | Accepted answer
  
  That is only logical. Your `tag` will be null so your query is actually
  `{% set entries = craft.entries.relatedTo(null).order('title').limit(null) %}` and that results in
  `{% set entries = craft.entries.order('title').limit(null) %}`
  So in the end you will fetch all entries.
  This is what you need
  
  ```
  {% set tag = craft.tags.slug(tag).first %}
  <!-- check if the tag exists -->
  {% if tag %}
      <!-- the length filter actually fetches all entries -> executes the query and caches all entries
      so you can just fetch them in the beginning. You'll gain no performance boost with that filter -->
      {% set entries = craft.entries.relatedTo(tag).order('title').limit(null).find() %}
      {% if entries %}
          {% for entry in entries %}
              <!-- code here you fetched entries -->
          {% endfor %}
      {% else %}
          <!-- you have a tag but no entries related to that tag -->
      {% endif %}
  {% else %}
      <!-- you have no tag -> you won't find any entries -->
  {% endif %}
  
  ```
  
  
  ---
  
  