1

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 ?

2
  • Your first thought was my first thought, too. Have you tried it?
    – Brad Bell
    Commented Oct 5, 2015 at 18:14
  • I think it is working, yes... just testing a bit more. Commented Oct 5, 2015 at 18:30

1 Answer 1

2

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 %}
1
  • 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. Commented Oct 5, 2015 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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