简体   繁体   中英

Gatsby how to redirect all www. to non www

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM