<p>You could also set up a route for <code>news/{slug}</code> to point to your news template, and have it do the check to see if the entry in <code>segment_2</code> is valid, and do something based on that.</p>

<p>I did something like this for my "members" pages:</p>

<pre><code> {% set thisProfile = craft.users.id(craft.request.segment(2))[0] %}

 {% if not thisProfile %}
    {% redirect siteUrl %}
 {% endif %}
</code></pre>

CC BY-SA 3.0
2015-10-05T18:51:34.980Z
Nice, I like this, thanks khalwat! These news articles are actually within categories, so in the template, if the Entry doesn't exist, I can redirect to the category. This seems a more logical place for this stuff than in the 404 template.
Phil Gyford
CC BY-SA 3.0
2015-10-05T18:25:11.180Z
2015-10-05T18:25:11.180Z
andrew.welch
<p>I'm having trouble setting things up to redirect some old news articles a certain way, while still serving existing articles:</p>

<blockquote>
  <p>If a page exists at <code>/news/my-article</code> then it should be served as normal.</p>
  
  <p>If that URL <em>doesn't</em> exist (ie, all non-functioning <code>/news/*</code> URLs), I want the user to be redirected to <code>/news</code>.</p>
  
  <p>All 404s that aren't within <code>/news</code> should be handled as normal.</p>
</blockquote>

<p>My first thought was to use a <code>redirect</code> tag in the 404 template, only if the request URL matches <code>/news/*</code>... but I'm not sure if that's too late in the process. By that time, has a 404 header already been sent, so the user would get 404 -> 301 -> 404 ?</p>

CC BY-SA 3.0
2015-10-05T18:14:23.683Z
Your first thought was my first thought, too. Have you tried it?
Brad Bell
CC BY-SA 3.0
2015-10-05T18:30:23.213Z
I think it is working, yes... just testing a bit more.
Phil Gyford
<redirect><404>
Redirecting certain 404s to a page, handling all others as normal
# Redirecting certain 404s to a page, handling all others as normal

(Asked by: username_0 on 2015-10-05)

I'm having trouble setting things up to redirect some old news articles a certain way, while still serving existing articles:

> If a page exists at `/news/my-article` then it should be served as normal.
>
> If that URL *doesn't* exist (ie, all non-functioning `/news/*` URLs), I want the user to be redirected to `/news`.
>
> All 404s that aren't within `/news` should be handled as normal.

My first thought was to use a `redirect` tag in the 404 template, only if the request URL matches `/news/*`... but I'm not sure if that's too late in the process. By that time, has a 404 header already been sent, so the user would get 404 -> 301 -> 404 ?

**Tags:** redirect, 404
> **Comments:**
> **username_1**: Your first thought was my first thought, too. Have you tried it?  
> **username_0**: I think it is working, yes... just testing a bit more.  

---

## 1 answers

### Answer by username_2 on 2015-10-05. Score: 2 | Accepted answer

You could also set up a route for news/{slug} to point to your news template, and have it do the check to see if the entry in segment_2 is valid, and do something based on that.
I did something like this for my "members" pages:
 {% set thisProfile = craft.users.id(craft.request.segment(2))[0] %}

 {% if not thisProfile %}
    {% redirect siteUrl %}
 {% endif %}

> **Comments:**
> **username_0**: Nice, I like this, thanks khalwat! These news articles are actually within categories, so in the template, if the Entry doesn't exist, I can redirect to the category. This seems a more logical place for this stuff than in the 404 template.  

---

