preface

In “A blog with VuePress + Github Pages”, we build a blog with VuePress and see what the final TypeScript Chinese document looks like.

This article is about SEO and redirection.

The problem

The first I use making Pages service site is established, the address is: mqyqingfeng. Making. IO/learn – types… .

Considering making access speed problems at home, I again on Gitee synchronization, the address is: mqyqingfeng. Gitee. IO/learn – types…

Then I decided to set up my own site, ts.yayujs.com

Then I thought, how can I not use HTTPS? Hence the new address: ts.yayujs.com

On top of that, yayujs.com is also the site…

This suddenly produced 5 addresses, so I decided to unify into one, which is convenient for memory and collection, and convenient for SEO optimization, will not lead to repeated collection, points away due search traffic.

unified

So which one?

Must be their own server and domain name first, otherwise money wasted……

Then because HTTPS is more SEO friendly, for example, Baidu search engine believes that sites with the same weight, using HTTPS protocol pages are more secure, ranking will be treated first, Google also recommends using HTTPS:

Google will prefer HTTPS web pages over the equivalent HTTP web pages as standard pages

So we use HTTPS.

As for yayujs.com/, considering this will be used as yuba’s personal blog page, I just pointed to this site first because it was not developed yet, so this address will remain the same and will be his personal blog in the future.

So the final unified address was ts.yayujs.com

JS redirect

GitHub Pages and Gitee Pages set up site Pages, because it is not their own server and domain name, there is no way to directly through domain name redirection or Nginx redirection methods, so we simply JavaScript to determine the domain name, Then location.href jumps to the new address:

// config.js
module.exports = {
    title: 'TypeScript4 Chinese document '.description: The latest TypeScript official documentation translation,TypeScript Chinese manual, provides TypeScript from the beginning to the advanced system learning tutorial..head: [['script', {}, ` (function() { if (location.href.indexOf('github.io') > -1 || location.href.indexOf('gitee.io') > -1) { location.href =  'https://ts.yayujs.com' } })(); `]]}Copy the code

Note that JavaScript location redirection should be the last of all redirection methods to be considered, as shown in the Documentation for Google Search Center:

Use JavaScript redirects only if you cannot perform server-side redirects or meta Refresh redirects. Although Google will try to render every url that Googlebot reaches, it may fail to render for a variety of reasons. This means that if you set up a JavaScript redirect and Google is unable to render it, Google may never see the redirect.

Nginx redirect

HTTP redirects HTTPS

Next we’ll use Nginx to redirect HTTP to HTTPS. This is what VuePress blog optimised to enable HTTPS. We’ll rewrite this using the rewrite statement in Nginx:

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
  			rewrite ^(.*)$ https://$host$1 permanent;

        location ^~ /learn-typescript/{ alias /home/www/website/ts/; } location / { alias /home/www/website/ts/; index index.html; }}Copy the code

Rewrite (rewrite) : this is a 301 redirection. (rewrite) : permanent (rewrite) : this is a 302 redirect.

If you need to change the url of a web page displayed in search engine results, it is recommended that you use permanent server-side redirection whenever possible. This is the best way to ensure that Google searches and users are directed to the right web page. The 301 and 308 status codes indicate that the web page has been permanently moved to its new location.

301 redirects are the only way to avoid any negative impact on site rankings.

Yayujs.com redirectwww.yayujs.com

To search engines, yayujs.com and www.yayujs.com are different sites, which makes sense, since www.yayujs.com is the equivalent of ts.yayujs.com as a subdomain, but if the domain is different, the content is the same. This will cause the search engine to make two entries, which will affect the natural traffic of both addresses, so we need to redirect one to the other.

Do you want to redirect from yayujs.com to www.yayujs.com or www.yayujs.com?

In fact, it doesn’t matter, with or without the WWW, SEO does not have any impact, this is more of a personal preference.

An example is given in the Google Search Center documentation:

Assume that users can access your web page in one of the following ways:

  • example.com/home
  • home.example.com
  • www.example.com

Choose one of these sites as the canonical site and use 301 redirects to direct traffic from other sites to your preferred site.

If you want yayujs.com to redirect to www.yayujs.com, you can modify the Nginx configuration as follows:

server {
  listen 443 ssl;
  server_name yayujs.com www.yayujs.com;
  if($host ! ='www.yayujs.com') {
   	rewrite ^/(.*)$ https://www.yayujs.com/$1permanent; }}Copy the code

If you want www.yayujs.com to redirect to yayujs.com, you can modify the Nginx configuration as follows:

server {
  listen 443 ssl;
  server_name yayujs.com www.yayujs.com;
  if ($host = 'www.yayujs.com') {
    rewrite ^/(.*)$ https://yayujs.com/$1permanent; }}Copy the code

However, it should be noted that although it is the same for SEO, we may encounter some differences when doing projects. For example, when dealing with cookie-related content, we can only modify the cookies of the current domain and the parent domain due to the same origin policy of cookies. The cookies of ts.yayujs.com and www.yayujs.com, for example, are isolated, but the cookies of ts.yayujs.com and yayujs.com are not.

series

Blog building series is the only practical series I have written so far. It is estimated to be about 20 articles, explaining how to use VuePress to build and optimize a blog and deploy it to GitHub, Gitee, private server and other platforms. This is the 28th article in a series at github.com/mqyqingfeng…

Wechat: “MQyqingfeng”, add me Into Hu Yu’s only readership group.

If there is any mistake or not precise place, please be sure to give correction, thank you very much. If you like or are inspired by it, welcome star and encourage the author.