Deprecated 301.txt Redirects

Note

This feature has been deprecated. It is highly recommended you update to use a Routing file.

301 redirects are inside the 301.txt file at the root of your built files. Redirects are specified using the following syntax:

old-url new-url

Redirect to external domains and wildcard matching with *. Here’s an example file:

301.txt
/about.html /about/
/category/* /new-category
/should-have-been-elsewhere/ https://example.com

Redirects will only occur if the redirect-from page/location no longer exists within the site. If the page/location still exists within your site, this will be served instead of performing a redirect.

Note

You can redirect to, but not from a URL fragment (e.g. #example-heading). The fragment is not passed to the server so, it cannot be processed by the redirect.

Migrating to a Routing file

301.txt redirects still work to ensure backwards compatibility for older sites. Once a .cloudcannon/routing.json file has been added 301.txt will be ignored. 301.txt offers a subset of the options available in the Routing file. This feature will not be receiving any further improvements.

Upgrading will give access to the following improvements:

  1. Regex redirects and regex parameters in the location
  2. Redirects run before all other behaviour allowing easier configuration
  3. More redirect status codes
  4. Customise headers on existing routes
  5. And more coming soon

For the above example, here is the equivalent:

.cloudcannon/routing.json
{
   "routes": [
      {
         "from": "/about.html",
         "to": "/about/",
         "status": 301,
         "substitutions": false,
         "forced": false
      },
      {
         "from": "/category/*",
         "to": "/new-category",
         "status": 301,
         "substitutions": false,
         "forced": false
      },
      {
         "from": "/should-have-been-elsewhere/",
         "to": "https://example.com",
         "status": 301,
         "substitutions": false,
         "forced": false
      }
   ]
}
Was this article helpful? or Suggest an improvement >

Related articles