Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/converting_rewrite_rules.xml
1 views
1
<!--
2
Copyright (C) Igor Sysoev
3
Copyright (C) Nginx, Inc.
4
-->
5
6
<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
7
8
<article name="Converting rewrite rules"
9
link="/en/docs/http/converting_rewrite_rules.html"
10
lang="en"
11
rev="2">
12
13
14
<section name="A redirect to a main site">
15
16
<para>
17
People who during their shared hosting life used to configure
18
<i>everything</i> using <i>only</i> Apache&rsquo;s .htaccess files,
19
usually translate the following rules:
20
21
<programlisting>
22
RewriteCond %{HTTP_HOST} example.org
23
RewriteRule (.*) http://www.example.org$1
24
</programlisting>
25
26
to something like this:
27
28
<programlisting>
29
server {
30
listen 80;
31
server_name www.example.org example.org;
32
if ($http_host = example.org) {
33
rewrite (.*) http://www.example.org$1;
34
}
35
...
36
}
37
</programlisting>
38
</para>
39
40
<para>
41
This is a wrong, cumbersome, and ineffective way.
42
The right way is to define a separate server for <literal>example.org</literal>:
43
44
<programlisting>
45
server {
46
listen 80;
47
server_name example.org;
48
return 301 http://www.example.org$request_uri;
49
}
50
51
server {
52
listen 80;
53
server_name www.example.org;
54
...
55
}
56
</programlisting>
57
58
<note>
59
On versions prior to 0.9.1, redirects can be made with:
60
<programlisting>
61
rewrite ^ http://www.example.org$request_uri?;
62
</programlisting>
63
</note>
64
65
</para>
66
67
</section>
68
69
70
<section>
71
72
<para>
73
Another example.
74
Instead of the &ldquo;upside-down&rdquo; logic &ldquo;all that is not
75
<literal>example.com</literal> and is not <literal>www.example.com</literal>&rdquo;:
76
77
<programlisting>
78
RewriteCond %{HTTP_HOST} !example.com
79
RewriteCond %{HTTP_HOST} !www.example.com
80
RewriteRule (.*) http://www.example.com$1
81
</programlisting>
82
83
one should simply define <literal>example.com</literal>, <literal>www.example.com</literal>,
84
and &ldquo;everything else&rdquo;:
85
86
<programlisting>
87
server {
88
listen 80;
89
server_name example.com www.example.com;
90
...
91
}
92
93
server {
94
listen 80 default_server;
95
server_name _;
96
return 301 http://example.com$request_uri;
97
}
98
</programlisting>
99
100
<note>
101
On versions prior to 0.9.1, redirects can be made with:
102
<programlisting>
103
rewrite ^ http://example.com$request_uri?;
104
</programlisting>
105
</note>
106
107
</para>
108
109
</section>
110
111
112
<section id="converting_mongrel_rules"
113
name="Converting Mongrel rules">
114
115
<para>
116
Typical Mongrel rules:
117
118
<programlisting>
119
DocumentRoot /var/www/myapp.com/current/public
120
121
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
122
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
123
RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]
124
125
RewriteCond %{REQUEST_FILENAME} -f
126
RewriteRule ^(.*)$ $1 [QSA,L]
127
128
RewriteCond %{REQUEST_FILENAME}/index.html -f
129
RewriteRule ^(.*)$ $1/index.html [QSA,L]
130
131
RewriteCond %{REQUEST_FILENAME}.html -f
132
RewriteRule ^(.*)$ $1.html [QSA,L]
133
134
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
135
</programlisting>
136
137
should be converted to
138
139
<programlisting>
140
location / {
141
root /var/www/myapp.com/current/public;
142
143
try_files /system/maintenance.html
144
$uri $uri/index.html $uri.html
145
@mongrel;
146
}
147
148
location @mongrel {
149
proxy_pass http://mongrel;
150
}
151
</programlisting>
152
</para>
153
154
</section>
155
156
</article>
157
158