Nginx bind domain name and configure www domain name 301 redirection

Source: Internet
Author: User
Recently it was easy to use. I found some information on the Internet and recorded it here. it is a memo. For Apache. htaccess and ASP/PHP scripts

Recently it was easy to use. I found some information on the Internet and recorded it here. it is a memo. For Apache. htaccess and ASP/PHP script targeting methods, refer to host domain name www adaptive 301 redirection method.

For example, if we have a domain name example.com and then configure our Nginx server, we want to bind two domain names, example.com without www, and www.example.com. To bind a domain name, you can perform the following configuration in the server {} section of your Nginx configuration file.


 server { listen 80; server_name example.com www.example.com; }


After this configuration, both example.com and www.example.com point to our server. although there is nothing like this, it is unfriendly to search engines and is not conducive to website SEO, therefore, we need to use 301 to redirect one domain name to another.

Introduce the $ host variable in the Nginx server section, which refers to the host name (domain name) used by the current visitor to access the host ). Next, we can use the if condition statement with the rewrite permanent method to perform 301 ING.


# Example 1: redirect example.com to www.example.com server {listen 80; server_name example.com www.example.com; if ($ host! = 'Www .example.com ') {rewrite ^ /(. *) $ http://www.example.com/#1 permanent ;}# Example 2: directs www.example.com to example.com server {listen 80; server_name example.com www.example.com; if ($ host! = 'Example. com') {rewrite ^/(. *) $ http://example.com/#1 permanent ;}}


The above two examples are used to determine whether the current request host domain meets our requirements. if it does not meet our requirements, the request path/content following it will be intercepted and appended to the desired url, 301 permanent orientation (permanent) at the same time ). Of course, what we use here is not equal! =. If equals is used, the equal sign (=) is used to determine and change the condition to the host domain we don't expect.

Note the following issues:

1. the if statement and parentheses of the preceding configuration file must be separated by a space. otherwise, Nginx will report nginx: [emerg] unknown directive "if ($ host" in... Error.

2. Nginx condition judgment is not equal! =, Equal to =. Note that there is only one equal sign here. if it is written as =, Nginx will report nginx: [emerg] unexpected "=" in condition in... Error.

3. 301 the browser may have a memory effect, for example, IE, after permanent redirection to the configuration is successful. So once the page is configured and accessed using the browser, after you change the 301 redirection configuration, the page may still be the previous redirection. we recommend that you clear the browser cache or try to access other pages, or in the url? The question mark is followed by some random parameters to avoid the cache memory effect of the browser.

After the configuration is complete, you can useNginx-s reloadCommand to smoothly update the configuration (without restarting Nginx ).

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.