.htaccessルールをnginxに移行しようとしています。SOとURLリライターに関するほとんどすべての質問を試しましたが、成功しませんでした。要するに、私は次の動的URLを変換したいと思います:
から
[1] - https://vc.test/results.php?url=ngo-service
[2] - https://vc.test/jobs.php?d=17&t=oil-&-gas
[3] - https://vc.test/jobs.php?d=17
に
[1] - https://vc.test/ngo-service
[2] - https://vc.test/17/oil-&-gas
[3] - https://vc.test/17
この問題を解決するために助けを求めてください。
私のnginxの取り組み
server {
listen 127.0.0.1:80;
listen 127.0.0.1:443 ssl http2;
ssl_certificate_key "d:/winnmp/conf/opensslCA/selfsigned/vc.test+4-key.pem";
ssl_certificate "d:/winnmp/conf/opensslCA/selfsigned/vc.test+4.pem";
server_name vc.test;
root "d:/winnmp/www/vc";
## Access Restrictions
allow 127.0.0.1;
deny all;
autoindex on;
location / {
index index.html index.htm index.php;
try_files $uri $uri.html $uri/ @extensionless-php;
if ($query_string ~* "fbclid="){
rewrite ^(.*)$ /$1? redirect;
break;
}
if ($query_string ~* "url="){
rewrite ^(.*)$ /%1? redirect;
rewrite ^/(.*)$ /results.php?url=$1 permanent;
break;
}
rewrite ^/([0-9]+)/(.*)?$ jobs.php?d=$1&t=$2 break;
rewrite ^/([0-9]+)?$ jobs.php?d=$1 break;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ \.php$ {
try_files $uri =404;
include nginx.fastcgi.conf;
include nginx.redis.conf;
fastcgi_pass php_farm;
fastcgi_hide_header X-Powered-By;
}
}
あなたのif ($query_string
ブロックが何のためにあるのかわからないので、私はそれらを無視します。
rewrite...last
書き換えられたURIを別のlocation
ブロックで処理する場合に使用し.php
ます。たとえば、URIが。で終わる場合などです。すべてのNginxURIは、先頭/
で始まります。たとえば、use/jobs.php
ではなくusejobs.php
です。
rewrite
ステートメントのリストをlocation /
ブロックに配置すると、一致するものが見つかるまで順番に評価されます。一致するものが見つからない場合、try_files
ステートメントが評価されます。それがリライトモジュールの仕組みです!!
ただし、最初の書き換えルールは一般的すぎるため、try_files
ステートメントによって実行されることを意図したURIの一部が破損する可能性があります。より良い解決策は、すべてのrewrite
ステートメントを同じ名前のlocation
ブロックに配置することです。
例えば:
index index.html index.htm index.php;
location / {
try_files $uri $uri.html $uri/ @rewrite;
}
location @rewrite {
if (-f $document_root$uri.php) {
rewrite ^ $uri.php last;
}
rewrite ^/([0-9]+)/(.+)$ /jobs.php?d=$1&t=$2 last;
rewrite ^/([0-9]+)$ /jobs.php?d=$1 last;
rewrite ^/([^/]+)$ /results.php?url=$1 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
...
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加