Apache-2.2

使用 MultiViews 為語言配置 Apache 內容協商

  • September 26, 2013

我試圖讓該MultiViews選項在 Apache 中工作,以根據Accept-Language請求中提供的內容改變返回給瀏覽器的內容。

我有以下配置:

Alias /multiviewstest "C:/MultiViews Test"

<Directory "C:/MultiViews Test">
   Options MultiViews
   AllowOverride None
   Order allow,deny
   Allow from all
</Directory>

在我的C:\MultiViews Test目錄中,我有以下文件:

  • spam.html
  • foo.html.en

當我請求返回http://localhost/multiviewstest/spam的內容時spam.html。以下是請求和響應標頭:

Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Date: Fri, 08 May 2009 11:07:54 GMT
Server: Apache/2.2.10 (Win32)
Content-Location: spam.html
Vary: negotiate
TCN: choice
Last-Modified: Fri, 08 May 2009 10:48:34 GMT
Etag: "0-4-469645ec81e70;469645ff5a5d8"
Accept-Ranges: bytes
Content-Length: 4
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

Content-Location和響應標頭表示Vary已正確啟動。TCN``MultiViews

我已將英語配置為在瀏覽器中顯示語言的唯一首選語言;Accept-Language en請求中設置了標頭。當我請求http://localhost/multiviewstest/foo.html返回 404 響應時。根據我對Apache 用於語言協商的文件命名約定的理解,我希望foo.html.en返回文件的內容。

以下是請求和響應標頭:

Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Date: Fri, 08 May 2009 11:08:39 GMT
Server: Apache/2.2.10 (Win32)
Content-Length: 221
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1

以下是請求的訪問日誌中顯示的內容:

127.0.0.1 - - [04/May/2009:10:28:24 +1200] "GET /multiviewstest/foo.html HTTP/1.1" 404 221

從錯誤日誌中:

[Mon May 04 10:28:24 2009] [error] [client 127.0.0.1] Negotiation: discovered file(s) matching request: C:/MultiViews Test/foo.html (None could be negotiated).

為什麼語言的內容協商沒有正確啟動?是否有一些我忽略的配置?

您的配置中是否存在正確的語言/擴展關係?

AddLanguage en .en
LanguagePriority en fr de
ForceLanguagePriority Fallback

FWIW,我遇到了類似的問題,使用了適當的 AddLanguage 等指令。最終我意識到問題是特定於 PHP 的。

出於某種原因,我在 FilesMatch 指令中使用了 SetHandler:

<FilesMatch \.php$>
   SetHandler application/x-httpd-php
</FilesMatch>

切換到一個簡單的 AddType 解決了這個問題:

AddType application/x-httpd-php .php

引用自:https://serverfault.com/questions/3079