# 通过路由重定向内部链接

**URL:** <https://meta.discourse.org/t/redirecting-internal-links-via-routes/236019>\
**Category:** Product Support\
**Created:** [2022 年8 月 12 日 18:19 UTC](https://meta.discourse.org/t/redirecting-internal-links-via-routes/236019 "2022-08-12T18:19:54Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

作者： ![attj](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@attj](https://meta.discourse.org/u/attj)\
发布日期： [2022 年8 月 12 日 18:19 UTC](https://meta.discourse.org/t/redirecting-internal-links-via-routes/236019/1 "2022-08-12T18:19:54Z")

</div>

我在网站上设置了以下重定向：[example.com/[0-9]+/[0-9]+](http://example.com/%5B0-9%5D+/%5B0-9%5D+) → [example.com/tag/$1-$2，其中](http://example.com/tag/$1-$2%EF%BC%8C%E5%85%B6%E4%B8%AD) $1 和 $2 是来自先前正则表达式的值。我编写了下面的简单插件，并且在 URL 栏中输入 [example.com/[0-9]+/[0-9]+](http://example.com/%5B0-9%5D+/%5B0-9%5D+) 时可以完美运行。但是，如果在 discourse 中的帖子中提供相同的链接，重定向将不起作用。有什么方法可以实现这一点？

```plaintext

Discourse::Application.routes.append do
    get '/:year/:id', to: redirect('/tag/%{year}-%{id}') , constraints: {year: /[0-9]+/, id: /[0-9]+/}
end

```

---

_[View the full topic](https://meta.discourse.org/t/redirecting-internal-links-via-routes/236019)._
