当前位置:网站首页>Popular explanation [redirection] and its practice

Popular explanation [redirection] and its practice

2022-06-24 16:25:00 Programmer fish skin

Three minutes , Take you to learn and practice domain name redirection

Hello everyone , I'm fish skin , Share today Redirect Little knowledge , And my practice of domain name redirection in Tencent cloud development .

Evil rise

Before , I developed a programming navigation website , Put the website on Tencent cloud development , Cloud hosting ( Containers ) The way to deploy and maintain . And bought a domain name code-nav.cn, And in the background of cloud development Access the service in , The subdomain name of the domain name www.code-nav.cn Associated with the container where the website files are stored , Configuration is shown in figure :

Cloud development website access configuration

then , You can go through the website www.code-nav.cn Visit the website .

Visit website

however , Soon , I found a serious problem .

Many students want to visit my website , however , Because the URL they entered is code-nav.cn, Omit the URL prefix www, Make the website inaccessible . Also let me lose a batch of users .

first battle —— Domain name configuration

To solve this problem , It's very simple , Before that was configuration www Subdomains point to containers , Then add another configuration in the background of cloud development , Directly purchase the domain name ( Parent domain name )code-nav.cn It also points to the container , That's it ?

The configuration is as follows :

Cloud development website access configuration

such , Whether or not the user enters www Prefix , Can visit our website !

Visit website

It seems perfect , But at the moment, , There are still some problems with the website .

Existing problems

First , belt www With or without www It's actually two different websites , Although for users , It feels like visiting the same website . But for search engines , Spiders will identify them as two different websites , And respectively include the content of the website under these two paths , It leads to weight dispersion . Although it has little influence on websites with small traffic , But for big sites , This is a problem that has to be dealt with .

Besides , visit code-nav.cn( No www) The user response , The data on the website could not be loaded . This is because , Tencent developed WEB Secure domain name restrictions , Only domain names in the white list are allowed to access cloud resources ( data 、 Documents, etc. ), therefore , Still in Security configuration in , Fill up code-nav.cn domain name .

Cloud development security configuration

Although it's normal now , But if there are still some and www The logic of strong correlation of websites , For example, it is necessary to judge that the URL visited by the user must be www.code-nav.cn To log in , Then you have to modify the code , A little thoughtless , It will lead to some functional problems .

To solve these problems , We can use Redirect technology .

Redirect

Redirection is a very broad concept , That is to say, through various methods, all kinds of network requests are reoriented to other locations , Like web redirection 、 Domain redirection 、 Data packet redirection, etc .

In website development , There are too many scenarios for redirection , For example, when the user is not logged in , The URL it entered will automatically jump to the login page ; When users visit the old web address , Automatically jump to the new page . Redirection is not just a tour guide , And an overbearing security guard .

therefore , Many large sites will use redirection Technology . Like visiting Baidu baidu.com, Press F12 Check out the developer console , You can see the URL through 302 Redirect , Auto jump to www.baidu.com.

Redirect

That's the question , What is 302 Redirect ?

Take a look at common redirections HTTP Status code .

Redirect HTTP Status code

About redirection HTTP The status code is mainly 301、302、303、307、308, The most common is 301 and 302, You can see MDN The official interpretation of them .

301 Is a permanent redirect (Moved Permanently) Indicates that the requested resource has been permanent Moved to by Location Head designated url On , It's fixed it won't change , The search engine will correct... Based on the response .

and 302 It's a temporary transfer (Moved Temporarily, perhaps Found), Indicates that the requested resource is temporary Moved to by Location Head designated URL On . The browser will redirect to this URL, However, the search engine will not update the link of this resource .

although 301 and 302 Can input the user's web address A, Change to reset the backward URL B, But they are different :

  • The difference between search engines :301 It means the original address A The resource for has been removed , Never have access to , When search engine grabs content, it will send URL A Replace all with B; and 302 It's a web address A Still alive , Search engines will be crawling websites B New content at the same time , Keep the URL A The record of .
  • Security :302 Jump has the risk of website hijacking , Leading to website embezzlement .

Fight again —— Cloud development redirection practice

After learning about redirection , Let's try how to achieve redirection , And how to implement domain name redirection in cloud development .

There are many ways to achieve redirection , A lot depends on what you use web The server , such as Nginx、Apache、Tomcat etc. , Generally, you can add several configurations to the server .

My programming navigation website is in a container way , Deployed on the cloud hosting function provided by cloud development . I put the developed website file and provide web Service Nginx The servers are packaged together , Made into containers , therefore , You can think of each container as a small server , Independent operation .

To support redirection , Just change it Nginx Configuration of . For example, here I choose to add 301 Permanent redirection , The configuration file is as follows :

server {
  listen 80;
  # gzip config
  gzip on;
  ...

  root /usr/share/nginx/html;
  include /etc/nginx/mime.types;

  #  Add redirection 
  if ($http_host ~ "^code-nav.cn") {
    rewrite  ^(.*)    https://www.code-nav.cn permanent;
  }
}

No need to write and remember Nginx To configure , Directly use the visual interface to generate :

Nginx Visual configuration

See this article for details : Easy to handle Nginx Configuration code artifact !

Other server configurations are also available in the documentation , No more details here .

then , Create a new version on cloud hosting , Release a new container , That's it !

New version

Check the effect , visit code-nav.cn, The site has been redirected to www.code-nav.cn, perfect !

View network requests

summary

Last , Review the whole process of domain name redirection in Tencent cloud development , It includes the following steps :

  1. stay Access the service Add the parent domain name to the site ( Cloud hosting, etc )
  2. stay Security configuration Add the parent domain name to the whitelist
  3. stay web Add redirection configuration to the server
  4. New version , Deployment of release

The whole process is very simple ~

If this article is helpful , Please do give me a Fabulous Support !️

原网站

版权声明
本文为[Programmer fish skin]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/04/20210426135912701c.html