All page with 404 redirect to homepage

Hello everyone. Help me figure out the problem. Can’t redirect all non-existent pages(error page) to homepage. Adding to Nginx config does not help. I’ve tried everything. As far as I understand, you can do a manual redirect in the site.yaml config but how?

redirects:
    /error: '/'

not work

Can I ask why you want to do this? 404 pages have a very clear and useful purpose. It might seem helpful to users, but simply redirecting them will have all kinds of unintended effects.

2 Likes

My site doesn’t need a 404 page. I need a simple 301 redirect of any 404 page to the main page. This is a common practice that I implemented via nginx config or htaccess. But in this cms I encountered a problem that I cannot solve.

AFAIK this is far from common practice :thinking: Also you will have issues with SEO if any invalid link redirects to a home page - I believe crawlers would mark all such pages as duplicates :man_shrugging:

I’m not here about SEO issues :slightly_smiling_face:
I need help setting up a redirect. There was one similar topic on this forum, but there was no solution there either.

Hi, @SupaVlad, you can try the following:

  1. make sure the error plugin is enabled
  2. create a page template in your theme called error.html.twig
  3. put this code
    {% if http_response_code(404) %}
        {{ redirect_me(home_url, 301) }}
      {% else %}
        <h1>{{ 'PLUGIN_ERROR.ERROR'|t }} {{ header.http_response_code }}</h1>
        <p>{{ page.content|raw }}</p>
    {% endif %}
    
2 Likes

You’re The Best
works like a clock

1 Like

I wanted to do the same thing for a small portfolio site; thanks @b.da for the solution!

One thing I might add for anyone concerned that this is a “wrong“ way to handle 404s, Is that perhaps a 303 might be more in keeping with the spirit of HTTP codes than a 301 or 302. According to MDN, 303 “indicates that the redirects don’t link to the requested resource itself, but to another page.”

No idea on how this may or may not affect SEO, but for my purposes that’s not really a concern, either.