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?
Das wird 'http: // example.com/file.html' jedoch nicht zu" http: // example.com/file "umleiten. –
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
Das hat auch nicht funktioniert: while/wird nicht nach/index umgeleitet,/foo leitet immer noch nach/foo/index weiter –