# 如何将所有404页面重定向到主页？

**URL:** <https://meta.discourse.org/t/how-to-redirect-all-404-pages-to-home/235187>\
**Category:** Self-hosting\
**Created:** [2022年八月5日 05:24 UTC](https://meta.discourse.org/t/how-to-redirect-all-404-pages-to-home/235187 "2022-08-05T05:24:08Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![nehakakar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nehakakar/32/317424_2.png) [@nehakakar](https://meta.discourse.org/u/nehakakar)\
**Post date:** [2023年八月8日 19:59 UTC](https://meta.discourse.org/t/how-to-redirect-all-404-pages-to-home/235187/15 "2023-08-08T19:59:07Z")

</div>

> [@riteshsaini](#):
>
> 由于 Discourse 不使用 htaccess，我不确定在哪里可以找到解决方案。任何帮助都将不胜感激

您可以使用“自定义”功能将所有损坏的 404 页面重定向到主页或其他页面。  
转到“ **自定义** ” \> “ **主题** ” \> “ **创建新主题** ”部分 \> 为您的主题命名，例如“ **404 重定向** ”，并将“ **基础主题** ”设置为“ **默认** ”。  
然后创建新主题 \> 编辑 CSS/HTML。在 HTML 部分，创建一个 JavaScript 代码段来处理重定向。  
粘贴此代码。

```plaintext
<script type="text/discourse-plugin">
  api.onPageChange((url, title) => {
    // 将所有 404 页面重定向到主页
    if (url.includes('/404')) {
      window.location.href = '/';
    }
  });
</script>

```

请根据您的需要调整页面名称。阅读此 [将旧 URL 重定向到新的 Discourse URL](https://meta.discourse.org/t/redirect-old-forum-urls-to-new-discourse-urls/20930)

---

_[View the full topic](https://meta.discourse.org/t/how-to-redirect-all-404-pages-to-home/235187)._
