#Is there a way to automatically redirect the root URL in an Astro Dev server to the base URL?

17 messages · Page 1 of 1 (latest)

sullen quest
#

If I set a different base in the Astro config file, the pages/index.astro would be served at the base URL (/my_custom_base). When running the dev server, it is possible to navigate to the root of the server domain (/), which only produces an Astro page that says "In your site you have your base path set to /my_custom_base. Do you want to go there instead?"

Is there a way or configuration for this page to automatically redirect to the base URL instead of requiring me to click the link? Maybe some Astro configuration in the config file which would add a <meta http-equiv="refresh"> in this file to automatically redirect. Or simply serve up a page with only the meta tag.

PS: I have tried redirects: { '/': '/my_custom_base' } but this doesn't work at the root URL and it creates a redirect loop at the base URL.

sullen quest
#

Hello, anyone?

sullen quest
#

Still a problem, would be nice to have a solution. Especially with Astro deployed in Vercel, we cannot inject extra files at the root path.

kindred wraithBOT
#

Houston, we have a problem... and <@&1129102257422610512>, you’re our mission control! 🚀

latent topaz
#

It should work

somber mango
#

They say they've already tried that

latent topaz
#

Are you using a static or a hybrid/server application?

#

Yeah but from experience, that should be it, I've had the same with no issues

sullen quest
#

Static, but my setup is unconventional. I have a base directory setting which is not the root, so src/index.astro will become dist/base/index.html (not dist/index.html). The problem is, that deployment systems like Vercel look for dist/index.html and upon not finding any, mydomain.vercel.app throws a 404 at the domain root. The actual page is at mydomain.vercel.app/base and I want mydomain.vercel.app to redirect to it.

This is not possible with the Astro config's redirects option as far as I can tell.

#

@latent topaz ideas?

latent topaz
#

Why do you need this base to begin with

#

You're definitely not the first person deploying to vercel, so what's breaking for you that's working fine for everyone else

sullen quest
#

Client reasons 🤣

sullen quest
#

@latent topaz I might have a solution, but riddle me this first. When I configure an internal redirect redirects why is it adding the domain of site in the redirect file? Example: { '/source': '/destination' } becomes:

<meta http-equiv="refresh" content="0;url=https://mysite.com/destination">

I want it to remain as:

<meta http-equiv="refresh" content="0;url=/destination">

There's nothing in the docs (you linked above) about this oddity. Is there a special config to avoid adding the site value in the redirect destination URL?

sullen quest
#

Anywayyyyyyyyyyyy........

#

SOLUTION:

Add a "parent folder" redirect:

redirects: { "../index": "/base" }

Note, if you have site configured, the redirect file will have ${site}/base. If you're deploying to a different domain (like a development domain), unfortunately you have to hardcode it in the redirect (this problem, I filed in Github as a bug):

redirects: { "../index": "https://my-dev-domain.com/base" }

Caveats:

  • This won't work in Astro Dev server and Astro Preview server, but it will work in any simple HTML static site server, and works beautifully in Vercel.
  • Becomes absolute redirect when site is configured, and won't work correctly with dynamically generated domains. See bug link above.