Adding redirects
Store the routes for your redirects in a ROUTER
file in the root of your project (no file extension necessary). ROUTER
files are part of Surge Plus. When you publish, you’ll be prompted to upgrade your project right from the CLI.
You can make this file using your favourite text editor, or right from the command line using:
touch ROUTER
Now, you can setup custom redirects within this file. There are three pieces of information required, all on the same line:
- The type of redirect, determined using the HTTP status code
- The original URL pattern you’d like to redirect
- The new URL pattern you’d like to redirect to
Basic redirect
A basic example:
301 /contact /about
Now, your old Contact page URL page will automatically redirect to your About page.
This example redirect has a 301 status code, which is a permanent redirect. The options for this are:
Status Code | Redirect Type |
---|---|
301 | Moved Permanently |
307 | Moved Temporarily |
Redirect patterns
What if you don’t just have one redirect to make, but many? You may also add a redirect pattern, which will catch and redirect all URLs following a certain format. For example:
301 /my-blog/:title /articles/:title
Now, any post previously under /my-blog/
, like /my-blog/my-first-post
, will be redirected to /articles/my-first-post
automatically. :title
is used in this example, but you can name the pattern variables anything you’d like. Here’s how you might redirect the URLs from an old WordPress blog to your new static Jekyll site:
301 /:year/:month/:day/:slug /blog/:slug
Remote redirects
You can also redirect to a different URL altogether:
307 /blog https://medium.com/surge-sh
Multiple redirects
If you’d like to add additional redirects, you can put one on each line.
301 /:year/:month/:day/:slug /blog/:slug
301 /contact /about
307 /code http://github.com/surge-sh