Example of 301 redirect configuration for domain name and directory in Nginx _nginx

Source: Internet
Author: User
Tags sub domain

301 Redirect is not unfamiliar, sometimes there is a need to redirect a directory whole to a level two domain name, or the top-level domain request without WWW is all redirected to the two-level domain name with www. If it is Apache, it needs to be configured. Htaccess,nginx is not supported and needs to be implemented using rewrite directives in the configuration file.

Top-level domain name redirect to www

Copy Code code as follows:

server {
server_name jb51.net;
Rewrite ^/(. *) $ http://www.jb51.net/$1 permanent;
}

As configured on, so jb51.net requests will be redirected to the www.jb51.net,301 redirect is helpful for SEO. This configuration is used by most people.

www level two domain name redirect to top-level domain

Copy Code code as follows:

server {
server_name www.jb51.net;
Rewrite ^/(. *) $ http://domain.com/$1 permanent;
}

Rivers and lakes are rumored that the weight of the top-level domain name than the WWW two domain name is high, some seoer will require the operation dimension must be transferred to the WWW request to the top-level domain name, and the above practice is the opposite.

Directory redirection

Copy Code code as follows:

if ($request _filename ~ nginxjiaocheng/) {
Rewrite ^ http://www.jb51.net/nginx/? permanent;
}

directory jump new domain name
Copy Code code as follows:

if ($request _filename ~ nginx/) {
Rewrite ^ http://nginx.jb51.net/? permanent;
}

Nginx This column is too hot, intend to specially get a domain name.

With: Domain name between the 301 jump and test methods

One, the same root domain name and the sub domain name jumps between

The first case: access to the root domain name redirect to the www subdomain (vhost is usually bound by a number of domain names, this can be precisely positioned to a domain name, does not cause the domain name to jump to this domain name)

server {
server_name www.jb51.net jb51.net;
if ($host = ' jb51.net ') {
Rewrite ^/(. *) $ http://www.jb51.net/$1 permanent;
}

Two, the different domain name between 301 jumps

The first case: access to a station to B station

Copy Code code as follows:
server {
server_name www.jb51.net;
Rewrite ^ (. *) http://www.jb51.net$1 permanent;
}

In the second case: not all redirects to the a station to the specified page

Copy Code code as follows:
server {
server_name www.jb51.net;
if ($host!= ' jb51.net ') {
Rewrite ^/(. *) $ http://www.jb51.net/$1 permanent;
}
}

If you write in the first server segment
will also be redirected when using IP access

301 REDIRECT status on-line detection tool: http://web-sniffer.net/

The return of this sentence is only 301 redirect success:

Copy Code code as follows:
status:http/1.1 Moved Permanently

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.