Nginxリライトテクノロジー
構文を書き直します:
rewrite {
规则} {
定向路径} {
重写类型}
rewrite ^/(.*) http://www.test.com/$1 permanent;
リライトリライトタイプ:
Lastは、Apacheの[L]マークに相当し
ます。これは、書き換えブレークが完了したことを意味します。このルールが一致すると、一致は終了し、次のルールは一致しなくなります。
リダイレクトは、302の一時的なリダイレクトを返します。ブラウザアドレスにはリダイレクトされたURLアドレスが
永続的に表示されます。301永続的なリダイレクトを返すと、ブラウザアドレスはリダイレクト後にURLアドレスを表示します。
書き直しの例:
1. http://106.52.36.65/1024.htmlにアクセスするときは、http://106.52.36.65/index.php?id = 1024にリダイレクトします。
location / {
rewrite ^/(\d+)\.html$ /index.php?id=$1 redirect;
#rewrite ^/(\d+)\.html$ /index.php?id=$1 break;
}
curl http://106.52.36.65/1024.html -L
戻り結果:
C:\Users\v_lysvliu>curl http://106.52.36.65/1024.html -L
<pre>Array
(
[id] => 1024
)
C:\Users\v_lysvliu>
2. http://106.52.36.65/home/1024.htmlにアクセスするときは、http://106.52.36.65/index.php?id = 1024にリダイレクトします。
location / {
rewrite ^/home/(\d+)\.html$ /index.php?id=$1 redirect;
}
3. http://106.52.36.65/admin/1024.htmlにアクセスするときは、http://106.52.36.65/admin/index.php?id = 1024にリダイレクトします。
location / {
rewrite ^/(\w+)/(\d+)\.html$ /$1/index.php?id=$2 redirect;
}
4. http://106.52.36.65/home/12-31-2020.htmlにアクセスするときは、http://106.52.36.65/home/index.phpにリダイレクトしますか?
id=2020-12-31
location / {
rewrite ^/(\w+)/(\d+)-(\d+)-(\d+)\.html$ /$1/index.php?id=$3-$1-$2 redirect;
}
- http://106.52.36.65にアクセスするときは、http://www.baidu.comにリダイレクトしてください。
location / {
rewrite .* http://www.baidu.com permanent;
}
6. http://106.52.36.65:80にアクセスするときは、http://106.52.36.65:443にリダイレクトします。
location / {
if ($server_port ~* 80) {
rewrite .* http://106.52.36.65:443 permanent;
#rewrite .* http://$host:443 permanent;
}
}