Mercurial > hg > nginx-site
comparison xml/cn/docs/http/converting_rewrite_rules.xml @ 720:9934338f83af
Updated the Chinese documentation.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Thu, 11 Oct 2012 10:23:05 +0000 |
parents | 130fad6dc1b4 |
children |
comparison
equal
deleted
inserted
replaced
719:6a37df6078a1 | 720:9934338f83af |
---|---|
1 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | 1 <!-- |
2 Copyright (C) Igor Sysoev | |
3 Copyright (C) Nginx, Inc. | |
4 --> | |
5 | |
6 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | |
2 | 7 |
3 <article name="转换rewrite规则" | 8 <article name="转换rewrite规则" |
4 link="/cn/docs/http/converting_rewrite_rules.html" | 9 link="/cn/docs/http/converting_rewrite_rules.html" |
10 rev="1" | |
5 lang="cn"> | 11 lang="cn"> |
6 | 12 |
7 | 13 |
8 <section name="重定向到主站"> | 14 <section name="重定向到主站"> |
9 | 15 |
10 <para> | 16 <para> |
11 共享站点的管理员,习惯于<i>只</i>在Apache下使用.htaccess文件配置<i>所有</i>信息,通常会将下面规则 | 17 共享站点的管理员,习惯于<i>只</i>在Apache下使用.htaccess文件配置<i>所有</i>信息,通常会将下面规则 |
12 | 18 |
13 <programlisting> | 19 <programlisting> |
14 RewriteCond %{HTTP_HOST} nginx.org | 20 RewriteCond %{HTTP_HOST} example.org |
15 RewriteRule (.*) http://www.nginx.org$1 | 21 RewriteRule (.*) http://www.example.org$1 |
16 </programlisting> | 22 </programlisting> |
17 | 23 |
18 翻译成这样: | 24 翻译成这样: |
19 | 25 |
20 <programlisting> | 26 <programlisting> |
21 server { | 27 server { |
22 listen 80; | 28 listen 80; |
23 server_name www.nginx.org nginx.org; | 29 server_name www.example.org example.org; |
24 if ($http_host = nginx.org) { | 30 if ($http_host = example.org) { |
25 rewrite (.*) http://www.nginx.org$1; | 31 rewrite (.*) http://www.example.org$1; |
26 } | 32 } |
27 ... | 33 ... |
28 } | 34 } |
29 </programlisting> | 35 </programlisting> |
30 </para> | 36 </para> |
31 | 37 |
32 <para> | 38 <para> |
33 这种做法是错的,复杂而且低效。正确的方式是为<literal>nginx.org</literal>定义一个单独的服务器: | 39 这种做法是错的,复杂而且低效。正确的方式是为<literal>example.org</literal>定义一个单独的服务器: |
34 | 40 |
35 <programlisting> | 41 <programlisting> |
36 server { | 42 server { |
37 listen 80; | 43 listen 80; |
38 server_name nginx.org; | 44 server_name example.org; |
39 return 301 http://www.nginx.org$request_uri; | 45 return 301 http://www.example.org$request_uri; |
40 } | 46 } |
41 | 47 |
42 server { | 48 server { |
43 listen 80; | 49 listen 80; |
44 server_name www.nginx.org; | 50 server_name www.example.org; |
45 ... | 51 ... |
46 } | 52 } |
47 </programlisting> | 53 </programlisting> |
48 | 54 |
49 <note> | 55 <note> |
50 在0.9.1版本(含)以前,可以这样实现重定向: | 56 在0.9.1版本(含)以前,可以这样实现重定向: |
51 <programlisting> | 57 <programlisting> |
52 rewrite ^ http://www.nginx.org$request_uri?; | 58 rewrite ^ http://www.example.org$request_uri?; |
53 </programlisting> | 59 </programlisting> |
54 </note> | 60 </note> |
55 | 61 |
56 </para> | 62 </para> |
57 | 63 |
59 | 65 |
60 | 66 |
61 <section> | 67 <section> |
62 | 68 |
63 <para> | 69 <para> |
64 再举一个例子,处理一个和刚才相反的逻辑:既不是来自<literal>nginx.com</literal>,又不是来自<literal>www.nginx.com</literal>: | 70 再举一个例子,处理一个和刚才相反的逻辑:既不是来自<literal>example.com</literal>,又不是来自<literal>www.example.com</literal>: |
65 | 71 |
66 <programlisting> | 72 <programlisting> |
67 RewriteCond %{HTTP_HOST} !nginx.com | 73 RewriteCond %{HTTP_HOST} !example.com |
68 RewriteCond %{HTTP_HOST} !www.nginx.com | 74 RewriteCond %{HTTP_HOST} !www.example.com |
69 RewriteRule (.*) http://www.nginx.com$1 | 75 RewriteRule (.*) http://www.example.com$1 |
70 </programlisting> | 76 </programlisting> |
71 | 77 |
72 应该按下面这样分开定义<literal>nginx.com</literal>、<literal>www.nginx.com</literal>和其他站点: | 78 应该按下面这样分开定义<literal>example.com</literal>、<literal>www.example.com</literal>和其他站点: |
73 | 79 |
74 <programlisting> | 80 <programlisting> |
75 server { | 81 server { |
76 listen 80; | 82 listen 80; |
77 server_name nginx.com www.nginx.com; | 83 server_name example.com www.example.com; |
78 ... | 84 ... |
79 } | 85 } |
80 | 86 |
81 server { | 87 server { |
82 listen 80 default_server; | 88 listen 80 default_server; |
83 server_name _; | 89 server_name _; |
84 return 301 http://nginx.com$request_uri; | 90 return 301 http://example.com$request_uri; |
85 } | 91 } |
86 </programlisting> | 92 </programlisting> |
87 | 93 |
88 <note> | 94 <note> |
89 在0.9.1版本(含)以前,可以这样实现重定向: | 95 在0.9.1版本(含)以前,可以这样实现重定向: |
90 <programlisting> | 96 <programlisting> |
91 rewrite ^ http://nginx.com$request_uri?; | 97 rewrite ^ http://example.com$request_uri?; |
92 </programlisting> | 98 </programlisting> |
93 </note> | 99 </note> |
94 | 100 |
95 </para> | 101 </para> |
96 | 102 |