Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

All page with 404 redirect to homepage

Solved by Vadym View solution

Started by Vlad 3 years ago · 7 replies · 965 views
3 years ago

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?

TXT
redirects:
    /error: '/'

not work

3 years ago

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
3 years ago

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.

last edited 11/14/23
3 years ago

AFAIK this is far from common practice 🤔 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 🤷‍♂️

3 years ago

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

3 years ago Solution

Hi, @SupaVlad, you can try the following:

  1. make sure the error plugin is enabled
  2. create a page template in your theme callederror.html.twig
  3. put this code
    TWIG
    {% 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
last edited 11/15/23 by Vadym
2 years ago

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.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 9 hours ago
Support · by Anna, 3 days ago
2 59 12 hours ago
Support · by Justin Young, 13 hours ago
1 30 13 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago