2014-11-14 3 views
0

바니시가있는 모든 URL에 후행 슬래시를 추가하고 싶습니다 (301 리디렉션을 통해).바니시로 후행 슬래시를 추가하는 방법은 무엇입니까?

그러나 나는 어디에서나 온라인에서 아무 것도 찾을 수 없다는 것에 놀랐다.

이것은 내가 얻은 것 중 가장 가까운 것이지만, 쿼리 문자열이나 기타를 고려하지 않았기 때문에 분명히 깨졌습니다. 그 안에.

if (req.url !~ "/$") { 
    return (synth (751, "")); 
} 

... 내가

example.com/xyz?query=string을 설명 할

sub vcl_synth { 
    if (resp.status == 750) { 
    set resp.status = 301; 
    set resp.http.Location = "http://www.example.com" + req.url; 
    return(deliver); 
    } 
} 

테스트 케이스 ->www.example.com/xyz/?query=string (추가, WWW를 추가 /)

example.com/api/latest.json ->www.example.com/api/latest.json (WWW를 추가 , 추가하지 마시오 /)

+0

스택 오버 플로우로보기 - 많은 웹 사이트에서 후행 슬래시가 필요 없습니다. –

+0

나는 당신의 의견을 원하지 않는다. 나는 나의 질문에 대한 답을 원한다. – Tallboy

+0

그래서'example.com/xyz? query = string'을'example.com/xyz/? query = string'로,'example.com/api/ latest.json'를'example.com/api/ '로 리디렉션하고 싶습니다. latest.json /'등? 질문을 편집하여 리디렉션의 수행 방법에 대한 구체적인 예를 제시해주십시오. – Ketola

답변

1

여기에 당신은 내가이없는 니스 (4)로 번역 할 수 니스 3에 대한 해결책은 니스 4 편리하게 자신 :

sub vcl_recv { 
    if (req.http.Host !~ "^www\." || (
      req.url !~ {"(?x) 
       (?:/$)   # last character isn't a slash 
       |    # or 
       (?:/\?)  # query string isn't immediately preceded by a slash 
      "} && 
      req.url ~ {"(?x) 
       (?:/[^./]+$) # last path segment doesn't contain a . no query string 
       |    # or 
       (?:/[^.?]+\?) # last path segment doesn't contain a . with a query string 
      "}) 
    ) { 
     error 720; 
    } 
} 

sub vcl_error { 
    if (obj.status == 720) { 
     set obj.status = 301; 

     set obj.http.Location = "http://"; 
     set obj.http.Host = req.http.Host; 
     if (obj.http.Host !~ "^www\.") { 
      // for www. prefix 
      set obj.http.Host = "www." + obj.http.Host; 
     } 
     set obj.http.Location = obj.http.Location + obj.http.Host; 

     if (req.url ~ "(?:/[^./]+$)|(?:/[^.?]+\?)") { 
      // no . in last path segment before optional query string 
      if (req.url !~ "/$" && req.url !~ "\?") { 
       // no trailing slash and no query string 
       set obj.http.Location = obj.http.Location + req.url + "/"; 
      } else if (req.url ~ "[^/]\?") { 
       // no trailing slash and with query string, preserve it 
       set obj.http.Location = obj.http.Location + 
        regsub(req.url, "([^?]+)\?.*", "\1") + 
        "/" + 
        regsub(req.url, "[^?]+(\?.*)", "\1"); 
      } else if (obj.http.Host != req.http.Host) { 
       // trailing slash rule met, handle missing www. scenario 
       set obj.http.Location = obj.http.Location + req.url; 
      } 
     } else if (obj.http.Host != req.http.Host) { 
      // last path segment contains a . so handle missing www. scenario 
      set obj.http.Location = obj.http.Location + req.url; 
     } 
     set obj.response = "Moved Permanently"; 
    } 
} 

난 당신이뿐만 아니라 관심있는 다양한 URL을 행사 니스 테스트 케이스 파일이 :

varnishtest "Testing adding trailing slash" 

server s1 { 
    rxreq 
    txresp -body "hello world" 
} -repeat 4 -start 

varnish v1 -vcl+backend { 
    include "${pwd}/26921577.vcl"; 
} -start 

client c1 { 
    txreq -url "/document/" -hdr "Host: www.example.com" 
    rxresp 

    expect resp.status == 200 
    expect resp.body == "hello world" 
} -run 

client c2 { 
    txreq -url "/document" -hdr "Host: www.example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/document/" 
} -run 

client c3 { 
    txreq -url "/document/" -hdr "Host: example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/document/" 
} -run 

client c4 { 
    txreq -url "/document" -hdr "Host: example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/document/" 
} -run 

client c5 { 
    txreq -url "/xyz/?query=string" -hdr "Host: www.example.com" 
    rxresp 

    expect resp.status == 200 
    expect resp.body == "hello world" 
} -run 

client c6 { 
    txreq -url "/xyz?query=string" -hdr "Host: www.example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/xyz/?query=string" 
} -run 

client c7 { 
    txreq -url "/xyz/?query=string" -hdr "Host: example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/xyz/?query=string" 
} -run 

client c8 { 
    txreq -url "/xyz?query=string" -hdr "Host: example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/xyz/?query=string" 
} -run 

client c9 { 
    txreq -url "/api/latest.json" -hdr "Host: www.example.com" 
    rxresp 

    expect resp.status == 200 
    expect resp.body == "hello world" 
} -run 

client c10 { 
    txreq -url "/api/latest.json" -hdr "Host: example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/api/latest.json" 
} -run 

client c11 { 
    txreq -url "/api/latest.json?query=string" -hdr "Host: www.example.com" 
    rxresp 

    expect resp.status == 200 
    expect resp.body == "hello world" 
} -run 

client c12 { 
    txreq -url "/api/latest.json?query=string" -hdr "Host: example.com" 
    rxresp 

    expect resp.status == 301 
    expect resp.http.Location == "http://www.example.com/api/latest.json?query=string" 
} -run 

varnish v1 -expect client_req == 12 
+0

감사합니다 !! 그게 큰 도움이됩니다. – Tallboy

관련 문제