2014-11-14 3 views
0

Я хочу добавить конечную косую черту ко всем URL-адресам с лаком (через 301 переадресацию).Как добавить косые черты с лаком?

Я был удивлен, что ничего не мог найти в Интернете в любом месте.

Это был самый близкий я получил, но, очевидно, сломан, потому что он не учитывает строки запроса или что-то еще с. в этом.

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

...

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

Тестовые я хочу объяснить

example.com/xyz?query=string ->www.example.com/xyz/?query=string (добавить WWW, добавить /)

example.com/api/latest.json ->www.example.com/api/latest.json (добавить WWW , dont add /)

+0

посмотреть в переполнении стека - нет необходимости в трейлинг-косе для многих веб-сайтов. –

+0

Мне не нравится ваше мнение, я хочу получить ответ на свой вопрос. – Tallboy

+0

Итак, вы хотите перенаправить 'example.com/xyz? Query = string' в' example.com/xyz/? Query = string' и 'example.com/api/latest.json' на' example.com/api/ last.json/'и т. д.? Не могли бы вы изменить свой вопрос, чтобы дать конкретные примеры того, как вы хотите, чтобы перенаправление выполнялось. – Ketola

ответ

1

Вот решение для Лака 3, что вы могли бы перевести в Varnish 4. Я не Varnish 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

Смежные вопросы