Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Så här redirectar du din 404-sida till home page i WordPress

Dina webbplatsbesökare kommer att se ett 404 ”hittades inte”-fel när de försöker besöka en sida som inte finns. Det är inte särskilt användarvänligt, och vi tror att över 70 % av dessa användare kommer att lämna din webbplats om de inte snabbt hittar något innehåll som de är intresserade av.

Lyckligtvis kan du åtgärda detta genom att omdirigera din 404-sida till din hemsida. Detta enkla trick kan göra stor skillnad. Det hindrar människor från att lämna din webbplats direkt och ger dem en chans att hitta något de kommer att gilla.

I den här artikeln visar vi dig exakt hur du omdirigerar din 404-sida till din startsida i WordPress, precis som när du guidar en förlorad vän tillbaka till festen.

How to Redirect Your 404 Page to the Home Page in WordPress

Varför ska du redirecta din 404-sida till din home page i WordPress?

När en användare försöker besöka en page som inte finns på din website WordPress kommer WordPress att visa dem en 404 error page istället.

Ordalydelsen och appearance på denna page kan variera beroende på vilket theme you använder.

Default WordPress 404 page

De flesta användare som landar på standard 404-sidan kommer att lämna din site snabbt. Detta kan öka din totala avvisningsfrekvens och påverka din search engine ranking negativt.

Det är därför du bör ta dig tid att skapa en customize 404 page. Men tills du gör det kan du ta genvägen i den här tutorialen och redirecta användare till homepage.

I en perfekt värld skulle you också kunna redirecta specifika 404 error requests till de mest relevanta pages.

Med detta sagt, låt oss titta på hur man redirectar all 404 error till din home page. Vi kommer att täcka två metoder och också visa dig hur du skapar custom redirects för enskilda pages:

Vill du inte hantera 404 error och redirects själv? Låt experterna på WPBeginner Pro Services hjälpa dig! Vi erbjuder prisvärd Emergency WordPress Support för att snabbt lösa 404 error och redirect issues. Sluta stressa över problem med din website och ta hand om dem. Schemalägg tjänster för WordPress-support idag!

Metod 1: Redirect alla 404 error till home page i WordPress

För den här metoden kommer vi att använda All in One SEO för WordPress (AIOSEO). Det är det bästa tillägget för sökmotorsoptimering för WordPress som allow you att ställa in redirects, optimera ditt content för search engines, och mycket mer.

Det första du behöver göra är att installera och aktivera All in One SEO (AIOSEO). Mer detaljer finns i vår Step-by-Step guide om hur du installerar ett plugin för WordPress.

Note: Det finns en gratis version av All in One SEO, men vi kommer att använda Pro-versionen eftersom Redirect är ett Pro addon.

När pluginet är aktiverat måste du gå till All in One SEO ” Redirects.

Om du inte har ställt in redirects ännu måste du klicka på knappen ”Activate Redirects”.

Activating Redirects in All in One SEO

Detta installerar All in One SEO: s Redirects-modul.

Därefter måste du klicka på tabben ”Settings” högst upp på sidan. Denna page allow you to configure redirects in AIOSEO.

The AIOSEO Redirects Settings Page

Du måste rulla ner på sidan tills du hittar ”Advanced 404 Settings” section. Aktivera denna section genom att helt enkelt clicka på knappen toggle så att den blir blå.

Nu kommer du att kunna se några fler Settings. You should toggle the ”Enable standard 404 redirect” button to the on position. Se sedan till att alternativet ”Home Page” är valt.

Enabling Default 404 Redirect in AIOSEO

Notice att det också finns ett ”Custom URL” alternativ. Detta allow you to redirect 404 errors to any post or page. Glöm inte att klicka på knappen ”Save Changes” högst upp eller längst ner på sidan för att store dina inställningar.

Nu när dina besökare försöker besöka en URL på din website som inte finns, kommer de att tas till home page istället för att se standard 404-sidan.

Metod 2: Redirect alla 404 error till home page med hjälp av kod

För den här metoden använder vi WPCode, vilket gör det säkert och enkelt att lägga till anpassad kod i WordPress. Dessutom levereras det med ett stort bibliotek med användbara kodsnuttar, inklusive en som automatiskt omdirigerar 404-sidor till hemsidan.

För att komma igång behöver du installera och aktivera det kostnadsfria insticksprogrammet WPCode. Om du behöver hjälp kan du följa vår handledning om hur du installerar ett WordPress-plugin.

När pluginet är aktiverat går du till Code Snippets ” + Add Snippet från WordPress instrumentpanel.

Därefter söker du efter utdraget ”Redirect 404 to Homepage” i biblioteket, håller muspekaren över det och klickar på knappen ”Use snippet”.

Select the 'Redirect 404 to Homepage' snippet in the WPCode library

WPCode kommer sedan automatiskt att lägga till koden åt dig och välja rätt insättningsmetod.

WPCode automatically adds the code for you

Därefter behöver du bara växla från ”Inactive” till ”Active” och klicka på knappen ”Update”.

Activate snippet and click the Update button

Nu, när en page inte hittas, istället för att se en 404-sida, kommer besökarna att redirectas till din WordPress home page.

Om du föredrar att inte använda ett plugin kan du använda ett enkelt code snippet för att redirecta all 404 pages till homepage.

I den här metoden måste du add to kod till dina WordPress-filer. Om du inte har gjort det tidigare kan du läsa vår guide för nybörjare om hur du klistrar in snippor från webben i WordPress. Du kanske också gillar vår guide till mall-hierarkin i WordPress.

Först måste du skapa en new fil i din WordPress theme folder och döpa den till 404.php. Om ditt theme redan har en 404. php-fil, måste du editera den filen istället.

För att edit denna fil måste du ansluta till ditt WordPress webbhotell account med en FTP-klient eller deras filhanteringsverktyg.

FTP 404 php file

När du är ansluten till din website kommer du att kunna se filen 404.php i din WordPress themes folder.

Du måste add to följande kod som första rad i din 404.php-fil:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>

Spara sedan filen 404.php och ladda upp den på nytt till din temakatalog med FTP eller kontrollpanelen på ditt webbhotell. Därefter kommer dina 404-fel att omdirigeras till din hemsida.

Bonus: Ställ in Custom 404 Page Redirects med AIOSEO

Det bästa och mest sökmotorsoptimerande sättet att hantera 404 error är att redirecta visitorerna till den mest relevanta page, eller ej bara din homepage.

All in One SEO-pluginets funktion Redirects kan också spåra alla 404-page error och låta dig ställa in smarta 404-page redirects i WordPress.

Om du installerade och aktiverade AIOSEO när du följde metod 1, är allt du behöver göra att navigera tillbaka till All in One SEO ” Redirects page och klicka på fliken ’Settings’.

Därefter rullar du ner tills du ser sectionen ”Logging”. Dina 404 loggar är en post med URL:er som dina besökare har försökt använda för att besöka pages som inte längre finns på din site.

Se till att ”404 Logs” är togglad på så att den ser blå ut. Du kan också välja hur länge du vill att pluginet ska posta dina loggar genom att välja tidsperiod från dropdown-menyn.

Enable 404 logs

Glöm inte att clicka på ”Save Changes” innan du går vidare.

Efter det kan du klicka på alternativet ”404 Logging” i menyn för att se den senast öppnade 404 page.

Kom ihåg att eftersom du just aktiverat logging kanske det inte visas något direkt. Du kan behöva vänta upp till 24 timmar innan du ser några användbara insikter där.

Click 404 logs menu option

Column ”URL” visar you de pages som användarna försökte besöka men som inte finns.

Du kan omdirigera någon av dessa URL:er till en befintlig page genom att clicka på länken ”Add Redirect” i kolumnen ”Redirect”. Då öppnas en dropdown där du kan enter informationen om din redirect.

För bästa resultat måste du enter den mest relevanta sidan för den ursprungliga sökningen i boxen ”Target URL” och sedan click ”Add Redirect”.

Add home page redirect

Om du till exempel har tagit bort en tutorial om hur man startar en WordPress-blogg och sedan skrivit en ny, är det bäst att redirecta den gamla URL:en till det nya blogginlägget i stället för till din homepage.

För att add to flera 404-page redirects, följ bara samma steg som ovan.

Kom ihåg att du allmänt inte vill redirecta din 404-sida till din home page för alltid. Det är en bra tillfällig lösning tills du skapar en custom 404-siddesign som konverterar bättre eller ställer in enskilda redirects som tar användare till en relevant page som finns.

Expertguider om 404 error i WordPress

Nu när du vet hur du redirectar din 404-sida till din home page i WordPress kanske du gillar att se några andra guider relaterade till hur du hanterar 404-fel.

Vi hoppas att den här artikeln hjälpte dig att lära dig hur du redirectar din 404 page i WordPress. Du kanske också vill se vår ultimata guide till sökmotorsoptimering i WordPress och de bästa drag and drop page builders i WordPress för att skapa customize 404-sidor.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Avslöjande: Vårt innehåll stöds av våra läsare. Det innebär att om du klickar på några av våra länkar, kan vi tjäna en provision. Se hur WPBeginner finansieras, varför det är viktigt, och hur du kan stödja oss. Här är vår editoriala process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Den ultimata WordPress-verktygslådan

Få GRATIS tillgång till vår verktygslåda - en samling WordPress-relaterade produkter och resurser som varje professionell användare bör ha!

Reader Interactions

66 kommentarerLämna ett svar

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Ralph

    I have 2 or 3 blog posts that had to be deleted, but the urls are still present on Pinterest, forums, etc., so I redirected 404 to my homepage in hosting settings to not lose traffic. However, for any other non deleted 404 I have my own ”sorry” info and search bar.
    I really appreciate good 404s on people’s websites, as this often indicates that they really care. Redirecting to the homepage has its uses, but not every time I think.

    • WPBeginner Support

      That is an option if you prefer it :)

      Administratör

  3. Jiří Vaněk

    I think, but it’s just my personal opinion, that redirecting people to the homepage isn’t good. Instead, it’s better to create a humorous 404 with an apology that reduces visitor frustration and offers interesting content in the form of a 404 page where the latest content of the website or blog can be automatically inserted. This way, the visitor realizes the page no longer exists but also gets the opportunity to laugh at a well-crafted 404 page and receives alternative content as an option for what to do next.

    • WPBeginner Support

      That is another option, it depends on the site and what type of content but both are valid options :)

      Administratör

  4. Shamsher Khan

    Thanks for sharing this amazing tutorial.

    • WPBeginner Support

      You’re welcome!

      Administratör

  5. Mbah Chinedu

    Hello, the 301 redirect you do at php file in ftp, can’t I use a text to inform user that the page is going to redirect in some seconds and then allow it redirect. At least bounce rate still reduces, because google has no issue with soft 404 or 404 pages

    • WPBeginner Support

      You can create a page like that if you wanted but we do not have a beginner-friendly method for creating that with PHP.

      Administratör

  6. Mark Cavallo

    very quick and easy trick – saved me a heap of time manualy redirecting with new website. thank you.

    • WPBeginner Support

      Glad our guide could help :)

      Administratör

  7. Dave

    Please what if I don’t want it to go to the homepage, how do I set it to another custom page please?

  8. Mudassir

    Hello WPbeginner team,

    I am using Astra Pro and have already some code in my 404.php. Now my question is, where do I add the above code exactly? Should I remove all that existing code and add yours or how?

    Please help!

    • WPBeginner Support

      For this tutorial, you would replace the code in your 404 page with this code normally. As we state in the article, this should only be done in very specific cases

      Administratör

      • Mudassir

        Thank you, I replaced it and it works!

        • WPBeginner Support

          Glad it worked :)

  9. Adeola

    Your posts are always very helpful. Thank you

  10. Peter

    Is this going still to be shown as a 404 in the Google Search Console ?!

    • WPBeginner Support

      Hi Peter,

      No, it will send 301 header message to crawlers telling them that the content has moved permanently.

      Administratör

  11. Noble A. Ozogbuda

    Hello please how can i modify this code to redirect all my 404s pages to a specific page, not homepage?

  12. Mentik

    Work Perfect … thanks

  13. Jack

    It worked perfectly ! Thanks Syed :)

  14. Mohan

    I have just installed it and did some redirection. I hope it works the way I expect. Anyway, its a nice plugin. Thanks for the helpful post about it.

  15. Jeffrey Asamoah

    worked thanks!

  16. Jitendra Mahato

    hi… i want to know that how to redirect the 404 page to a custom page.I want to do that in my blog. Thank you. Hoping your answer. I used this code and doing well.

  17. Drake Bliss

    If you only want to redirect a specific url path, try this code below. First create a child theme if you do not have one and move 404.php to the the child theme.

    Edit the 404.php page in the child theme and paste this code into the very top of the 404.php page. You can insert more check for other paths you want to redirect,.

  18. yudi cahyadi

    hi there…i’m using chid theme but it doesn’t exist 404 file,, how to creat 404.php file using child theme not in main theme to insert code ..

    thank you..

    • umer tanveer

      Insert code to Main Theme

  19. eray

    thank u so much. it works ..

  20. Kaizur Ahmed

    Great! This code work fine.

  21. Liem Hoang

    Work perfectly! Thank admin so much, you have been saved my life. My website will no longer get error win WMT.

  22. Malindo My Id

    wow thankyou so much,, did you know thats my site have been redirect from spammer, makes my eror page 404 found until 1240 not found url. and now I was redirect to my home age.. you are good boys.. thankyou.. I like this page,, so useful for many blogger ..

    Malindo My Id from Indonesia

  23. Shoaib

    i read anywhere on web that 404s should not be redirected globally to the home page.

    whats your view ???

    • WPBeginner Support

      We agree that it is not the best practice. However, sometimes it may become necessary for some websites to do that.

      Administratör

  24. Sanjar

    Thanks alot

  25. Sid

    Is there any way that I can redirect specific 404 pages. Like I created a custom post type and I just want to redirect 404 pages on that post type. Will that be possible?

  26. Jaswinder

    Good and valuable information, this is what I was looking for.

    Thanks.

  27. Kumar

    This code is causing problem, when i post something new permalink gets shorten and does not contain complete post title.

    What shld i do?

  28. Miguel Guzman

    I have 1,500 404’s that is because i started using dates on the url at the beginning when i started using WordPress. I changed the permalinks and that’s when the 404 started. is really hard when you have so many redirect to map them individually. its easier to re direct then all to home page.

  29. Riccardo

    How can you redirect depending on the language the site is displaying?
    My website is in french and english, but when I use your code it goes only to one page for both languages.

    Thank you for your help!

    Riccardo

  30. slango20

    this is bad practice, very bad. If you have time to set up a wordpress site, you have time to put 404 Not Found into a 404.php at *least* instead of redirecting to the homepage with no explanation whatsoever

  31. jhelum

    fantastic… thanks a lot

  32. dcaryll

    Appreciate the quick explanation and answer. Thanks for the post.

  33. Roy

    I am very grateful for this. You saved me doing 200+ redirects. Worked a treat.

  34. P.D.

    Does anyone know if the code works as well for multisite installations?

    If so, does the 404 page bounce to the subdomain / subfolder / subsite or the main or root domain / folder / site?

  35. Vishal

    Thanks dude…. :)

  36. Prasanna

    I really thank full to you. My website here after won’t get crawl errors.

    Thanks a lot.

    :)

    • slango20

      those crawl ”errors” are perfectly normal and don’t affect your site’s ranking, just like it’s not a good idea to reply to all sms with the same response just because you received a few spam sms

  37. marco

    Thanks that help a lot.

  38. Dilpreet Bhatia Music

    Wow – That Was easy!!

  39. Filip Hajek

    There is a cool WP plugin ”404 Redirected”, whoch captures 404s for you and let’s you redirect them easily.

    • Eric

      Thanks for the comment! That helped me out a lot! The article’s has a good solution too but I don’t like messing with my code. Thanks again for the suggestion Flip!

      -Eric Out-

  40. Nikole

    What if I want t redirect to a specific page not the homepage?

    • Editorial Staff

      You can change line 3. Or better use the redirection plugin like we mentioned in the article.

      Administratör

  41. Rakesh

    It rocks!!! I used the code ( previously my theme given me a custom 404 page template, but I replaced with your few lines). Thank you.

  42. ud

    Thanks for your very useful information

  43. Arslanh

    I did this but it doesn’t seem to work. I have no knowledge of coding so please help me!

    • Editorial Staff

      It should work just fine if you follow the article how it is. This is not a very complicated piece of code.

      Administratör

  44. Julien

    I am really not convinced by the relevance of this. 404 means the page doesn’t exist anymore, 301 is a permanent redirect. Those 2 errors do not mean the same for search engines.

    • Rootpak

      I agree. I would not recommend redirecting all 404 pages to the home page. From SEO point of view it is not accepyable. It means that every not existing page was moved to home page. It can simply lead to problems how google sees your site.

  45. Andy Feliciotti

    I use smart 404 extension and I must say it’s amazing, it’ll find a post with a similar URL and redirect to it

  46. Clark Wimberly

    I should know better, but one time I created an infinite loop using something close to this. It was a site with few pages for mostly logged-in users, so I made my 404 template redirect to the user page. What I had forgotten is that I made my user page redirect to the 404 if you weren’t signed in. Totally my fault, and barely relevant, but there you go.

    • Editorial Staff

      Hah that’s funny. The only reason why we wrote this code was for a small one-page site which where we didn’t want users to peek around too much. Some folks are just curious and they start running search or checking out feeds etc. So we disabled search, feeds, and redirected 404 to the homepage.

      Administratör

Lämna ett svar

Tack för att du väljer att lämna en kommentar. Tänk på att alla kommentarer modereras enligt våra policy för kommentarer, och din e-postadress kommer INTE att publiceras. Vänligen använd INTE nyckelord i namnfältet. Låt oss ha en personlig och meningsfull konversation.