How use redirects for a path that doesn't exist?

I have the following Netlify site: https://willcarh.art. I’m trying to set up URL routing.

The site is built from a custom static-site generator and puts generated files into a src/ folder. However, index.html is still in the root. For example, the file page.html is actually src/page.html in the file tree. I’d like to set up the URL for the site to still be just /page.

Here’s a more concrete example. The index page for the blog can be found in /src/blog_index.html. I would the like the blog URL to be /blog, but point to the file /src/blog_index.html without changing the URL. A couple questions:

  • is it possible for the user to use URL /src/blog_index.html but have the URL resolve to /blog while serving /src/blog_index.html, even though there’s no actual file /blog or /blog.html?
  • do my internal hrefs to other site pages (e.g. the home page to the blog page) need to use /blog, then? It is easier during development to link to the actual page (e.g. /src/blog_index.html) so the site works locally
  • if I use redirects, and a page links to /src/blog_index.html, will /src/blog_index.html appear in the URL briefly before it is redirected to /blog?

I have tried the following to achieve this, none of which work because there’s no file to serve at /blog.html. In _redirects:

/src/blog_index /blog
/src/blog_index.html /blog
/src/blog /blog
/src/blog.html /blog

In netlify.toml:

[[redirects]]
	from = "/src/blog_index.html"
	to = "/blog"
	status = 200

Repo: GitHub - wcarhart/willcarh.art: Personal porfolio website, custom static site generator

Welcome to Netlify Forums @wcarhart

I created a basic version of your tree. netlify.toml reads like this

[[redirects]]
  from = "/blog"
  to = "/src/blog_index.html"
  status = 200
  
[[redirects]]
  from = "/blog/*"
  to = "/src/blog/:splat"
  status = 200

Another solution is to create your file tree exactly as you would like the URLs to appear rather than require rewrites and routing.

1 Like

Yes, I fixed this by changing my file tree so it matched the routing pattern I wanted.

2 Likes

Thanks for the sharing a solution here, @coelmay! @wcarhart, glad this is working for you :netliconfetti:

1 Like