Path: blob/main/xml/en/docs/http/converting_rewrite_rules.xml
1 views
<!--1Copyright (C) Igor Sysoev2Copyright (C) Nginx, Inc.3-->45<!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">67<article name="Converting rewrite rules"8link="/en/docs/http/converting_rewrite_rules.html"9lang="en"10rev="2">111213<section name="A redirect to a main site">1415<para>16People who during their shared hosting life used to configure17<i>everything</i> using <i>only</i> Apache’s .htaccess files,18usually translate the following rules:1920<programlisting>21RewriteCond %{HTTP_HOST} example.org22RewriteRule (.*) http://www.example.org$123</programlisting>2425to something like this:2627<programlisting>28server {29listen 80;30server_name www.example.org example.org;31if ($http_host = example.org) {32rewrite (.*) http://www.example.org$1;33}34...35}36</programlisting>37</para>3839<para>40This is a wrong, cumbersome, and ineffective way.41The right way is to define a separate server for <literal>example.org</literal>:4243<programlisting>44server {45listen 80;46server_name example.org;47return 301 http://www.example.org$request_uri;48}4950server {51listen 80;52server_name www.example.org;53...54}55</programlisting>5657<note>58On versions prior to 0.9.1, redirects can be made with:59<programlisting>60rewrite ^ http://www.example.org$request_uri?;61</programlisting>62</note>6364</para>6566</section>676869<section>7071<para>72Another example.73Instead of the “upside-down” logic “all that is not74<literal>example.com</literal> and is not <literal>www.example.com</literal>”:7576<programlisting>77RewriteCond %{HTTP_HOST} !example.com78RewriteCond %{HTTP_HOST} !www.example.com79RewriteRule (.*) http://www.example.com$180</programlisting>8182one should simply define <literal>example.com</literal>, <literal>www.example.com</literal>,83and “everything else”:8485<programlisting>86server {87listen 80;88server_name example.com www.example.com;89...90}9192server {93listen 80 default_server;94server_name _;95return 301 http://example.com$request_uri;96}97</programlisting>9899<note>100On versions prior to 0.9.1, redirects can be made with:101<programlisting>102rewrite ^ http://example.com$request_uri?;103</programlisting>104</note>105106</para>107108</section>109110111<section id="converting_mongrel_rules"112name="Converting Mongrel rules">113114<para>115Typical Mongrel rules:116117<programlisting>118DocumentRoot /var/www/myapp.com/current/public119120RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f121RewriteCond %{SCRIPT_FILENAME} !maintenance.html122RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]123124RewriteCond %{REQUEST_FILENAME} -f125RewriteRule ^(.*)$ $1 [QSA,L]126127RewriteCond %{REQUEST_FILENAME}/index.html -f128RewriteRule ^(.*)$ $1/index.html [QSA,L]129130RewriteCond %{REQUEST_FILENAME}.html -f131RewriteRule ^(.*)$ $1.html [QSA,L]132133RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]134</programlisting>135136should be converted to137138<programlisting>139location / {140root /var/www/myapp.com/current/public;141142try_files /system/maintenance.html143$uri $uri/index.html $uri.html144@mongrel;145}146147location @mongrel {148proxy_pass http://mongrel;149}150</programlisting>151</para>152153</section>154155</article>156157158