6

ディスク上のファイルの拡張子index.htmlは ,a.htmlです。http://example.com/ato load/var/www/a.htmlhttp://example.com/to loadのリクエストが必要です/var/www/index.html。他の URL を正規の URL にリダイレクトするhttp://example.com/a.html必要があるため、にリダイレクトする必要がありhttp://example.com/aます。

私の構成は次のようになります。

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

これはリダイレクトさ/a.htmlれ、ディスクから/aのロードに成功します。a.html

$ 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

しかし、それはに送信//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

書き換えルールを削除すると、からのリダイレクトが停止/a.htmlしますが、への/a送信も停止//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

なぜこれが起こるのでしょうか?.html両方の必要なもの (拡張子なしindex、URL なし) を同時にnginx にすることはできますか?

4

2 に答える 2

1

あなたの書き換えルールは逆になっていると思います。たぶんこれだけです(書き換えルールなし):

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

location = / {
    index index.html;
}

編集版:

申し訳ありませんが、私はあなたの説明を完全に理解していませんでした。私はそれを数回読み直し、これをテストしましたが、あなたがやろうとしていることに近づいているかもしれません:

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;
}

コード例 (改訂 3):

3回目は魅力だと思います。多分これはあなたの問題を解決しますか?

# 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;
}
于 2012-12-20T00:31:40.303 に答える
0

あなたはこのようなものを探していますか:

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

基本的に、これはa.html最初にファイルを試行し、失敗した場合はindex.html最後にを表示しようとし404ます。また、ファイルrestart nginxを編集した後も忘れないでください。vhost

于 2012-12-21T06:24:15.733 に答える