How do I do cross-domain redirects to github repositories from netlify?

Hello,

I have a website on netlify (https://ari-web.netlify.app/) with a
custom domain (https://ari-web.xyz/), I want to link a github repo
to a route (for example GitHub - TruncatedDinosour/website: my website), I already achieved this
in the browser, it works fine and redirects to GitHub - TruncatedDinosour/website: my website but I would like it to be git-clonable, but it is not and anything
I found on it did not work (for example HTTP/302 redirect):

ari@ari-gentoo ~ % git clone https://ari-web.xyz/git
Cloning into 'git'...
fatal: repository 'https://ari-web.xyz/git/' not found

I have access to DNS and can add custom redirects and all that stuff,
just whatever netlify allows, you can see the /git route in website/netlify.toml at terminal · TruncatedDinosour/website · GitHub

[[redirects]]
    from = "/git"
    to = "https://github.com/TruncatedDinosour/website"
    status = 301
    force = true

I found related stuff:

But they did not help

Is it even possible?

Hi @B00bleaTea

I would never have considered doing this, but it is.

I used _redirects (instead of netlify.toml)

/git/*   https://github.com/username/repository/:splat

I was then able to clone the repository locally.

So using the netlify.toml you would have

[[redirects]]
  from = "/git/*"
  to = "https://github.com/TruncatedDinosour/website/:splat"
  status = 301
  # Not sure if force is really necessary
  force = true

The reason is that GitHub does a redirect too i.e.

https://<your-site-name>/<redirect-name>/info/refs?service=git-upload-pack

so in order for the clone to work, you need to use a :splat to pass through the appended path.

The repository redirect blog post you linked to applies to internal redirects that GitHub does when a repository is renamed. The Stack Overflow thread, while seemingly related is related to git-server which doesn’t apply here.

2 Likes

Thank you!

I will try now :slight_smile:

It worked!

for example

ari@ari-gentoo tmp % git clone https://ari-web.xyz/gh/rys
Cloning into 'rys'...
warning: redirecting to https://github.com/TruncatedDinosour/rys/
remote: Enumerating objects: 115, done.
remote: Counting objects: 100% (115/115), done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 115 (delta 38), reused 105 (delta 28), pack-reused 0
Receiving objects: 100% (115/115), 27.79 KiB | 3.47 MiB/s, done.
Resolving deltas: 100% (38/38), done.

Thanks a lot :slight_smile: