How to set 301 permanent redirection for a domain name in Nginx

Source: Internet
Author: User
Under the macro control of the country, if a website accidentally leaves the country, it is helpless. in a harmonious society, everyone should do their best. Windows hosts outside China are expensive. Linux is much cheaper than windows because the system is free of charge. Although I have never used Linux before, I have bought a Linux VPS (

Under the macro control of the country, if a website accidentally leaves the country, it is helpless. in a harmonious society, everyone should do their best. Windows hosts outside China are expensive. Linux is much cheaper than windows because the system is free of charge. Although I have never used Linux before, I still bought a Linux VPS (I can't afford it on an independent server, and it's not necessary). So I learned how to install the Lnmp one-key installation package, everything went well. In the past few days, I have to change the website address. based on the SEO idea, I am going to get a 301 permanent redirection. so I found some information on the website and it was still quite smooth. I just got it done without any effort.

 

Let's talk about the method. I will not talk about the specific Lnmp installation, and I will not be refined. 301 redirection is mainly used to modify the core variable of nginx and the server function of the conf file of the target website.

For example, to redirect 2cto.com to www.2cto.com, modify the code as follows:

Server {
Server_name www.2cto.com 2cto.com;
If ($ host! = 'Www .2cto.com '){
Rewrite ^/(. *) $ http://www.2cto.com/#1 permanent;
Proxy_set_header Host "www.2cto.com ";
}
...
}

You can change the domain name to yours. Note the following two points:

1. permanent: this is the key, indicating permanent orientation;

2. the role of the following code:

Proxy_set_header Host "www.2cto.com ";

This code can be added, but the effect is different. If 2cto.com or Taobao is not input /. You can choose whether to add or not to this code as needed.

Note: After testing, the code proxy_set_header Host "www.2cto.com" is added. The Test will result in an error and you can jump to the internal page without this section. Hereby declare!

Note: Due to the editor, if the Chinese characters such as ", please change them to English characters" appear during code copying, otherwise the modification will fail.
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.