2012-12-19 6 views
6

Meine Dateien auf der Festplatte haben Erweiterungen: index.html, a.html. Ich möchte eine Anfrage für http://example.com/a laden /var/www/a.html und http://example.com/ laden /var/www/index.html. Ich möchte, dass jede andere URL zu einer kanonischen URL umleitet, also sollte http://example.com/a.html zu http://example.com/a umleiten.redirect/foo.html to/foo aber nicht/to/index in nginx

Meine Konfiguration sieht so aus:

rewrite ^(/.+)\.html$ $scheme://$host$1 permanent; 
location/{ 
    root /var/www; 
    try_files $uri.html $uri $uri/ =404; 
} 

Diese /a.html-/a nicht umleiten und beim Laden a.html von der Festplatte gelingen:

$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location 
Location: http://www.jefftk.com/food 
$ curl -s http://www.jefftk.com/food | grep ^Location 

Aber es sendet / zu /index:

$ curl -s -D- http://www.jefftk.com/pictures/ | grep ^Location 
Location: http://www.jefftk.com/pictures/index 
$ curl -s -D- http://www.jefftk.com | grep ^Location 
Location: http://www.jefftk.com/index 

Wenn ich die Rewrite-Regel entfernen Sie es nicht mehr /a.html-/a Umleitung sondern stoppt auch das Senden / zu /index:

$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location 
$ curl -D- -s http://www.jefftk.com/food | grep ^Location 
$ curl -D- -s http://www.jefftk.com/ | grep ^Location 
$ curl -D- -s http://www.jefftk.com/pictures/ | grep ^Location 

Warum sollte dies geschehen? Kann ich nginx zu beiden Sachen machen, die ich will (no .html Erweiterung, no index in URL) zur gleichen Zeit?

Antwort

1

Ich denke, Ihre Rewrite-Regel könnte rückwärts sein. Vielleicht einfach diese (keine Rewrite-Regel):

location/{ 
    try_files $uri.html $uri $uri/ =404; 
} 

location =/{ 
    index index.html; 
} 

editierte Version:

Sorry, ich war nicht Ihre Beschreibung Verstehen vollständig. Ich las es mehrmals und getestet dies und es könnte zu schließen, was Sie suchen zu tun:

location =/{ 
    try_files /index.html =404; 
} 

location = /index { 
    return 301 $scheme://$host; 
} 

location ~* \.html$ { 
    rewrite ^(.+)\.html$ $scheme://$host$1 permanent; 
} 

location/{ 
    try_files $uri.html $uri/ @backend; 
} 

location @backend { 
    # rewrite or do whatever is default for your setup 
    rewrite^/index.html last; 
    // or return 404; 
} 

CODE-BEISPIEL (REVISION 3):

Ich bin zum dritten Mal der Hoffnung ist ein Reiz. Vielleicht wird das dein Problem lösen?

# example.com/index gets redirected to example.com/ 

location ~* ^(.*)/index$ { 
    return 301 $scheme://$host$1/; 
} 

# example.com/foo/ loads example.com/foo/index.html 

location ~* ^(.*)/$ { 
    try_files $1/index.html @backend; 
} 

# example.com/a.html gets redirected to example.com/a 

location ~* \.html$ { 
    rewrite ^(.+)\.html$ $scheme://$host$1 permanent; 
} 

# anything else not processed by the above rules: 
# * example.com/a will load example.com/a.html 
# * or if that fails, example.com/a/index.html 

location/{ 
    try_files $uri.html $uri/index.html @backend; 
} 

# default handler 
# * return error or redirect to base index.html page, etc. 

location @backend { 
    return 404; 
} 
+0

Das wird 'http: // example.com/file.html' jedoch nicht zu" http: // example.com/file "umleiten. –

+0

Entschuldigung, ich habe Ihre Frage nicht vollständig verstanden, habe sie aber noch einmal gelesen und einige neue Regeln getestet. Ich habe den obigen Code überarbeitet, um weitere Standorte hinzuzufügen. – kchan

+0

Das hat auch nicht funktioniert: while/wird nicht nach/index umgeleitet,/foo leitet immer noch nach/foo/index weiter –

0

Sind Sie für so etwas suchen:

location/{ 
    try_files $uri.html $uri/index.html =404; 
} 

Im Grunde ist dies versuchen, die a.html Datei zuerst, wenn das es dann nicht versuchen, index.html und letzte Show ein 404. Bitte denken Sie auch an restart nginx, nachdem Sie Ihre vhost Datei bearbeitet haben.