
내 nginx.conf에 다음이 있습니다.
location ~* /collections.*?products/([^/]+)/?$ {
rewrite ^/collections.*?products/([^/]+)/?$ /$1.html;
rewrite ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3;
rewrite ^([^_]*)_(.*)$ $1-$2 permanent;
}
다음과 같은 요청을 다시 작성하려면
"/collections/products/someproduct/" to "/someproduct.html"
"/collections/products/some_product/" to "/some-product.html"
"/collections/products/some_other_product/" to "/some-other-product.html"
그러나 마지막 재작성 지시문(플래그 포함)이 일치하고 처리되는 경우에만 301 리디렉션이 발생합니다 permanent
(예: 두 번째 예). 다른 2개의 인스턴스에서는 302 임시 리디렉션이 발생합니다. 이 위치 블록에서 이러한 여러 재작성 지시문을 어떻게 처리하고 어느 것이 일치하는지에 관계없이 301 리디렉션을 반환할 수 있습니까? 모든 재작성 지시문에 영구 플래그를 지정하면 첫 번째 일치 후 처리가 중지됩니다.
답변1
에서 독립적으로 재귀적 _
으로 변환할 수 있습니다 .-
rewrite...permanent
예를 들어:
location ~* /collections.*?products/([^/]+)/?$ {
rewrite ^(.*)_(.*)$ $1-$2 last;
rewrite ^/collections.*?products/([^/]+)/?$ /$1.html permanent;
}
두 번째는 rewrite
첫 번째가 rewrite
더 이상 밑줄을 찾지 못한 후에만 실행됩니다. 보다이 문서이상.
답변2
302
상태 코드를 "예외"로 처리하고 다음을 사용하여 "잡을" 수 있습니다.http://nginx.org/r/error_page.
location ~* /collections.*?products/([^/]+)/?$ {
rewrite ^/collections.*?products/([^/]+)/?$ /$1.html;
rewrite ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3;
rewrite ^([^_]*)_(.*)$ $1-$2 permanent;
error_page 302 =301 @302to301;
}
location @302to301 {
return 300; # 300 is just a filler here, error_page dictates status code
#return 301 $sent_http_location;
}
기술이 저랑 비슷하네요301-302-redirect-w-no-http-body-text.nginx.conf, 따라HTTP 응답 본문 없이 301/302 리디렉션 생성에 대한 관련 질문.
내에서는 @302to301
위의 두 반환 문 중에서 선택할 수 있습니다. 그러나 return
코드는 이 핸들러의 컨텍스트와 관련이 없습니다. 위의 지시문은 후속 코드가 무엇인지에 관계없이 error_page
모든 302
코드가 변경되도록 보장하기 때문입니다.301
즉, return
위 두 명령문의 유일한 차이점은 HTTP 응답 본문의 내용입니다. 이는 어떤 브라우저에서도 301 응답에 대해 표시되지 않으므로 본문이 없는 더 짧은 버전을 사용하는 것이 좋습니다 return 300
.