The simple way of setting up a website is to add the entire website to IPFS, then use the root hash.
Quick example:
First create both index.html
and firstpost.html
and then add them to IPFS
$ ls
firstpost.html index.html
$ cat firstpost.html
Hello world
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="./firstpost.html">First Post</a>
</body>
</html>
$ ipfs add -r .
added QmePw8gVcBMb8x6kAep6aMBAX23hCSk6iZW3i9VKkiFhu1 tmp.YoFFC4RGK0/firstpost.html
added Qmf7mg1Xi1YH7Xeds6J7Dhz8SRvkMRBkP9LTwWSbedwK4h tmp.YoFFC4RGK0/index.html
added QmdKpDPHpNx6cLUWViHXLAzAwnKspYf7iJUMmr8HPcSdBQ tmp.YoFFC4RGK0
The root hash is the last hash, QmdKpDPHpNx6cLUWViHXLAzAwnKspYf7iJUMmr8HPcSdBQ
. This is the one you will send people. Since firstpost.html
is relatively linked, it’ll work to resolve it when people click on the link.
Now, let’s say we add another post to our example and then add to IPFS again
$ cat secondpost.html
Second post
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="./firstpost.html">First Post</a>
<a href="./secondpost.html">Second Post</a>
</body>
</html>
$ ipfs add -r .
added QmePw8gVcBMb8x6kAep6aMBAX23hCSk6iZW3i9VKkiFhu1 tmp.YoFFC4RGK0/firstpost.html
added QmXsDomV8zSZJ8tSsCiLTdWVV5k8ph3jKdSqfKJhk2P8pW tmp.YoFFC4RGK0/index.html
added QmYQTFLHtocZcbEphEpLbhRd19aj8bpKUR3q5pm2QBJZxR tmp.YoFFC4RGK0/secondpost.html
added QmUeWXkXy6ygANqw4NKnSYf41Mtn35LV6SupgHQdZ89UNt tmp.YoFFC4RGK0
When adding the site now, secondpost.html
is a new file and index.html
has changed, so the root hash is now instead QmUeWXkXy6ygANqw4NKnSYf41Mtn35LV6SupgHQdZ89UNt
. This is again the hash you would send people.
But sending everyone the new hash gets boring quickly, so you can use DNS to make people be able to just reload the page instead.
So set A
records of example.com
to point to a IPFS gateway (you can use our public gateways if you want, get the IP by pinging ipfs.io
a couple of times, and set the A records to point to our gateway hosts).
Once you have A records setup, you need to add a TXT
record as well, to define which hash to add. This TXT record needs to have the value dnslink=/ipfs/QmUeWXkXy6ygANqw4NKnSYf41Mtn35LV6SupgHQdZ89UNt
. That’s the hash the gateway will load when you load your domain.
Hope this explains how it works. Important point is to keep all links and references to other files relative, so it works both when visiting ipfs.io/ipfs/$yourHash
and example.com
.