Mercurial > hg > nginx-site
view xml/it/docs/http/converting_rewrite_rules.xml @ 2898:0b7e004b5061
nginx-1.23.2, nginx-1.22.1
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Wed, 19 Oct 2022 11:26:47 +0300 |
parents | 6303d4e343a8 |
children |
line wrap: on
line source
<!-- Copyright (C) Igor Sysoev Copyright (C) Nginx, Inc. --> <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> <article name="Convertire rewrite rule" link="/it/docs/http/converting_rewrite_rules.html" lang="it" translator="Angelo Papadia" rev="1"> <section name="Redirect ad un sito principale"> <para> Chi, nel corso della propria esperienza con host condivisi, e' sempre stato abituato a configurare <i>tutto</i> usando <i>solo</i> i file .htaccess di Apache, in genere converte le seguenti regole: <programlisting> RewriteCond %{HTTP_HOST} example.org RewriteRule (.*) http://www.example.org$1 </programlisting> in qualcosa tipo: <programlisting> server { listen 80; server_name www.example.org example.org; if ($http_host = example.org) { rewrite (.*) http://www.example.org$1; } ... } </programlisting> </para> <para> Si tratta di una soluzione errata, poco elegante e inefficiente. La soluzione corretta prevede la definizione di un server distinto per <literal>example.org</literal>: <programlisting> server { listen 80; server_name example.org; return 301 http://www.example.org$request_uri; } server { listen 80; server_name www.example.org; ... } </programlisting> <note> Nelle versioni antecedenti la 0.9.1, i redirect possono essere definiti con: <programlisting> rewrite ^ http://www.example.org$request_uri?; </programlisting> </note> </para> </section> <section> <para> Un altro esempio: invece della logica “upside-down”, vale a dire “tutto quello che non e' <literal>example.com</literal> ne' <literal>www.example.com</literal>”: <programlisting> RewriteCond %{HTTP_HOST} !example.com RewriteCond %{HTTP_HOST} !www.example.com RewriteRule (.*) http://www.example.com$1 </programlisting> e' meglio semplicemente definire <literal>example.com</literal>, <literal>www.example.com</literal>, e “tutto il resto”: <programlisting> server { listen 80; server_name example.com www.example.com; ... } server { listen 80 default_server; server_name _; return 301 http://example.com$request_uri; } </programlisting> <note> Nelle versioni antecedenti la 0.9.1, i redirect possono essere definiti con: <programlisting> rewrite ^ http://example.com$request_uri?; </programlisting> </note> </para> </section> <section id="converting_mongrel_rules" name="Conversione delle regole di Mongrel"> <para> Regole di Mongrel tipiche, quali: <programlisting> DocumentRoot /var/www/myapp.com/current/public RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f RewriteCond %{SCRIPT_FILENAME} !maintenance.html RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_FILENAME}/index.html -f RewriteRule ^(.*)$ $1/index.html [QSA,L] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.*)$ $1/index.html [QSA,L] RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L] </programlisting> andrebbero convertite in: <programlisting> location / { root /var/www/myapp.com/current/public; try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; } </programlisting> </para> </section> </article>