1

What is a way to redirect all requests from www.any-page to the non www. version? I've looked into createRedirect but that only allows the absolute path to be specified?

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
user737163
  • 431
  • 3
  • 9

1 Answers1

0

I've looked into createRedirect but that only allows the absolute path to be specified?

This is no true. In the examples you can see relatives redirections:

exports.createPages = async ({ graphql, actions }) => {
    const { createRedirect } = actions;

    createRedirect({
    fromPath: `/blog/recipes/*`,
    toPath: `/recipes/*`,
  });
}

Regarding your question: the kind of redirections you are trying to apply should be done in the server, not in the client, depending on your infrastructure you can use a simple .htaccess file to AWS Lambdas. For example, the following .htaccess will do the redirection you want:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Extracted from Generic htaccess redirect www to non-www

Place a file named .htaccess in the /static folder and Gatsby will do the rest.

You can also use gatsby-plugin-htaccess for a more succinct approach.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67