2012-12-19 5 views
6

ディスク上の私のファイルは、拡張子がindex.html,a.htmlです。 http://example.com/a/var/www/a.htmlhttp://example.com/をロードして/var/www/index.htmlをロードするリクエストが必要です。標準URLにリダイレクトする他のURLが必要なので、http://example.com/a.htmlhttp://example.com/aにリダイレクトする必要があります。/foo/foo.htmlをnginxの/ to/indexにリダイレクト

私の設定は次のようになります。これは/a/a.htmlをリダイレクトし、ディスクからロードa.htmlで成功し

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

$ 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/a.htmlからリダイレクトするだけでなく、/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 

これはなぜ起こるのでしょうか?私は両方のnginxを作ることができます(いいえ.htmlの拡張子、いいえ、indexのURLで)?

答えて

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

コード例(REVISION 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; 
} 
+0

これは 'http:// example.com/file.html'を' http:// example.com/file'にリダイレクトしません。 –

+0

申し訳ありませんが、私はあなたの質問を完全に理解していませんでしたが、もう一度読み直していくつかの新しいルールをテストしました。上記のコードを修正して追加の場所を追加しました。 – kchan

+0

これはうまくいきませんでした。/ fooは/ foo/indexにリダイレクトされますが、 –

0

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

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

基本的にこれは、それが失敗した場合、それは、その後index.htmlを試してみて、最後のショー404なり、最初のa.htmlファイルをしようとします。また、vhostファイルを編集した後は、​​に覚えておいてください。

関連する問題