Skip to content

Commit e0843d5

Browse files
committed
Update content based on merged code
Based on things provided by ipfs/kubo#8890
1 parent ba3beb8 commit e0843d5

File tree

4 files changed

+126
-97
lines changed

4 files changed

+126
-97
lines changed

docs/.vuepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ module.exports = {
225225
'/how-to/websites-on-ipfs/link-a-domain',
226226
'/how-to/websites-on-ipfs/introducing-fleek',
227227
'/how-to/websites-on-ipfs/static-site-generators',
228-
'/how-to/websites-on-ipfs/redirects-file-support'
228+
'/how-to/websites-on-ipfs/redirects-and-custom-404s'
229229
]
230230
},
231231
{
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
title: Redirects and custom 404s
3+
description: What the _redirect file is and how to use them with a website or single-page application (SPA) on IPFS.
4+
---
5+
# Redirects, custom 404s, and SPA support
6+
7+
::: callout
8+
This feature is new, and requires Kubo 0.16 or later.
9+
:::
10+
11+
This feature enables support for redirects, [single-page applications](#catch-all-and-pwa-spa-support), [custom 404 pages](#add-a-custom-404-page-to-your-website), and moving to IPFS-backed hosting [without breaking existing links](https://www.w3.org/Provider/Style/URI).
12+
13+
[[toc]]
14+
15+
## Evaluation
16+
17+
This feature is limited to websites hosted in web contexts with unique [Origins](https://en.wikipedia.org/wiki/Same-origin_policy) for content roots, e.g., [subdomain](/how-to/address-ipfs-on-web/#subdomain-gateway) or [DNSLink](/how-to/address-ipfs-on-web/#dnslink-gateway) gateways.
18+
19+
Redirect logic will only be evaluated if the requested path is not in the [DAG](/concepts/glossary/#dag). Any performance impact associated with checking for the existence of a `_redirects` file or evaluating redirect rules will only be incurred for non-existent paths. If there are any errors reading or parsing the `_redirects` file, the error codes will be returned with an HTTP 500 status code.
20+
21+
## How to set up
22+
23+
To define rules executed when requested path is not found in DAG, there must be a file named `_redirects` stored underneath the root CID of the website. This `_redirects` file must be a text file containing one or more lines that follow the format explained below.
24+
25+
## Format of the `_redirects` file
26+
27+
Each line contained within the `_redirects` file has 3 basic components:
28+
29+
```plaintext
30+
from to [status]
31+
```
32+
33+
1. The `from` path. This specifies the path to be redirected from.
34+
1. The `to` path. This specifies the path to be redirected to.
35+
1. The `status` component. This part is optional and specifies the HTTP status code that will be returned. (301, 404, etc.)
36+
37+
For example, if you removed `home.html` and want to temporarily redirect traffic from `home.html` page to your `index.html` page, the `_redirects` file should contain a line that looks something like this:
38+
39+
```plaintext
40+
/home.html /index.html 302
41+
```
42+
43+
44+
### Status codes
45+
46+
- `200` - OK (redirect will be treated as a rewrite, returning payload from alternative content path without changing the URL shown in the browser).
47+
- `301` - Permanent redirect (the default status).
48+
- `302` - Found (commonly used for temporary redirects).
49+
- `404` - Not found (defines custom 404 page).
50+
- `410` - Gone (the requested content has been permanently removed).
51+
- `451` - Unavailable for legal reasons.
52+
53+
### Placeholders
54+
55+
Placeholders are named variables that can be used to match path segments in the `from` path and inject them into the `to` path.
56+
57+
This is useful for redirecting users to their desired content, even if the way your website is organized changed .
58+
59+
For example, if I wanted to search for an article titled "hello world" that was written on June 15, 2022, I could search for it like this: `/posts/06/15/2022/hello-world` and be redirected to `/articles/2022/06/15/hello-world`
60+
61+
```plaintext
62+
/posts/:month/:day/:year/:title /articles/:year/:month/:day/:title 301
63+
```
64+
65+
There is also special catch-all placeholder named `:splat` which represents everything captured via `*`.
66+
67+
```plaintext
68+
/blog/* /new-blog/:splat 302
69+
```
70+
71+
### Compatibility
72+
73+
IPFS hosting supports only a subset of pre-existing standards supported by [Cloudflare](https://developers.cloudflare.com/pages/platform/redirects) and [Netlify](https://docs.netlify.com/routing/redirects/).
74+
There is no overwrite/shadowing: the file is evaluated only when requested path is not found in a [DAG](/concepts/glossary/#dag).
75+
76+
::: tip
77+
For more detailed information about supported features, check out the [`_redirects` file specification](https://github.com/ipfs/specs/blob/main/http-gateways/REDIRECTS_FILE.md).
78+
:::
79+
80+
81+
## Examples
82+
83+
### Catch all and PWA/SPA support
84+
85+
The `200` status will be treated as a rewrite, returning OK without changing the URL shown in the browser. This staus code can be used to build [Progressive Web Apps](https://en.wikipedia.org/wiki/Progressive_web_app) and [Single Page Applications](https://en.wikipedia.org/wiki/Single-page_application).
86+
87+
```plaintext
88+
/app/* /app/index.html 200
89+
```
90+
91+
Opening `/app/this-does-not-exist` will return HTTP 200 response with content from `/app/index.html`
92+
93+
### Redirect an old URL to a new place
94+
95+
The `301` status is a permanent redirect, this is the default status code used when no code is specified.
96+
Below two rules mean the same:
97+
98+
```plaintext
99+
/old/docs.html /new/documentation.html
100+
/old/docs.html /new/documentation.html 301
101+
```
102+
103+
The `302` status is commonly used for temporary redirects.
104+
105+
```plaintext
106+
/home /under-construction.html 302
107+
```
108+
109+
For advanced and catch-all redirects, see [Placeholders](#placeholders) below.
110+
111+
### Add a custom 404 page to your website
112+
113+
Since the `_redirects` is evaluated only when requested path does not exist,
114+
it is possibleto define a custom 404 page for your website:
115+
116+
```plaintext
117+
/* /custom-404.html 404
118+
```
119+
120+
With the above rule, opening `/this-does-not-exist` will return HTTP 404 Not Found error response with the payload of a custom error page defined in `custom-404.html`.

docs/how-to/websites-on-ipfs/redirects-file-support.md

Lines changed: 0 additions & 96 deletions
This file was deleted.

docs/how-to/websites-on-ipfs/single-page-website.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ description: Learn how to host a simple one-page website on IPFS and link up a d
77

88
In this tutorial, we will host a simple one-page website on IPFS and link up a domain name. This is the first step is a series of tutorials to teach web developers on how to build websites and applications using IPFS.
99

10+
::: callout
11+
If you are looking for [single-page application (SPA)](https://en.wikipedia.org/wiki/Single-page_application) support, see [redirects and custom 404s](/how-to/websites-on-ipfs/redirects-and-custom-404s) instead.
12+
:::
13+
1014
## Install IPFS desktop
1115

1216
IPFS desktop application is the easiest way to get up and running quickly with IPFS. The installation steps for IPFS desktop differ between operating systems. Follow the instructions for your system.
@@ -254,3 +258,4 @@ This project was designed to get you up and running quickly, but there are many
254258

255259
You may have noticed that when visiting [randomplanetfacts.xyz](http://randomplanetfacts.xyz), your browser redirects to [gateway.pinata.cloud/ipfs/QmW7S5HR...](https://gateway.pinata.cloud/ipfs/QmW7S5HRLkP4XtPNyT1vQSjP3eRdtZaVtF6FAPvUfduMjA). This isn't great for the user's experience, and it can cause issues with security certificates and other website validation methods. Also, this website is incredibly simple. There are no images, external stylesheets, or javascript files.
256260
If you're interested in building a more complex site using IPFS and securing it properly, [carry on with this tutorial series by hosting a multipage website on IPFS.](multipage-website.md)
261+

0 commit comments

Comments
 (0)