Redirects of gh-hosted pages to custom domain

The simplest solution is to use absolute URLs for all your assets.

right - you mean just point to {{ site.baseurl }}/wiki/ ? I am doing that currently, but if someone casually shares the link to just the wiki without the slash it will fail

It would have been possible to exclude a URL from the wildcard but that’s not possible in the proxy setup that you currently have.

could you be more specific about how I would do that?

No, I meant to refer the CSS/JS assets with absolute URLs. So instead of gitbook/foo.css you include the absolute path of where the resource actually exists.

This part is easily achievable using:

/foo /foo 200!
/* /bar 200!

In the above example, you’re rewriting /foo back to /foo. Since it’s the first rule in order, it’s given preference. And since Netlify only proxies once (doesn’t go on following the proxy target), it doesn’t create a redirect loop. The wildcard rule is below this so it’s given less priority. But anything that’s not /foo will be captured by wildcard redirect and would redirect elsewhere.

/foo /foo 200!
/* /bar 200!

so this is syntax for a _redirect file, no? how would this look for a netlify.toml file?

[[redirects]]
  from = "/foo"
  to = "/foo"
  status = 200
 force = true

[[redirects]]
  from = "/*"
  to = "/bar"
  status = 200
  force = true

This would be it. I still believe this is not going to work for you. But, if you do try, let us know how it goes.

you were right, I could not get it to work. I don’t know why this is so difficult. this is the toml-file: asellus-homepage-archive/netlify.toml at master · The-Asellus-Consortium/asellus-homepage-archive · GitHub - it’s now pointing directly at the actual subdomain

I was not sure whether the wiki config would affect the config in the main page, so I deactivated that other file completely.

I’ve kind of lost track of what all was happening in this thread and it’s got pretty confusing at this point.

Would you be able to write up a summary of the expected behaviour and your current configuration?

goal: subdomain redirect from wiki.asellus.org to Introduction | Asellus Wiki (including all wiki pages)

problem: redirect is working fine, but only to Introduction | Asellus Wiki ← slash at the end is needed, which is problematic

config: homepage, wiki (wiki config is turned off at the moment - I wanted a domain level rewrite, but it leads to infinite loop). I also tried adding the slash with asset optimization, Redirect options | Netlify Docs, but that didn’t work either.

I have the same problem with another site, so a global solution to this would be terrific. I am not 100% what the core issue is here, I would frankly also be happy if the site would work without that last slash, but I guess then I would have to do what you suggested and rewrite all links to content and assets to absolute URL. Not sure how I would accomplish that though, bc the wiki is autogenerated from markdown source files.

Awesome, thanks for that and sorry for the delay in getting back here.

So there are multiple things to try:

  1. I wonder why you have this redirect at all:
[[redirects]]
  from = "https://asellus.org/wiki"
  to = "https://wiki.asellus.org"
  status = 200
  force = true

The one that you’ve after that, should be enough to handle all pages, I suppose.

  1. The real problem actually is the way assets are linked in your setup. You’re loading the files from relative to the current directory:

image

And because the trailing slash after wiki is absent, the root of the site is being considered as the current directory.

I believe, you could solve this with the <base> tag:

2 Likes

The base url element was exactly what I was looking for and solved all my problems. Thanks for your help and patience, and sorry for the messy thread - feel free to edit.

1 Like