withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.56k stars 2.47k forks source link

Redirect from the page gives empty page and response 200 #6795

Closed teinett closed 1 year ago

teinett commented 1 year ago

What version of astro are you using?

2.2.1

Are you using an SSR adapter? If so, which one?

node

What package manager are you using?

npm

What operating system are you using?

mac

What browser are you using?

firefox, chrome

Describe the Bug

I migrate old Tumblr blog to AstroJS. I have old links domain.com/post/123, which I would like to save. At the end I will have pages:

I want to have 301 redirect from domain.com/post to domain.com/archive.

What I did:

The code of src/pages/post.astro:

---
Astro.redirect('/archive');
---

In the browser I have 2 options:

My config astro.config.mjs:

import { defineConfig } from 'astro/config';
import mdx from "@astrojs/mdx";
import node from "@astrojs/node";

export default defineConfig({
  trailingSlash: 'never',
  integrations: [mdx()],
  output: 'server',
  adapter: node({
    mode: "standalone"
  })
});

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-e5p1e6

Participation

matthewp commented 1 year ago

You need to return your redirect.

---
return Astro.redirect('/archive');
---
teinett commented 1 year ago

@matthewp Thanks a lot! It works now!