为使用MultiView的语言configurationApache内容协商

我试图让Apache的MultiViews选项能够根据请求中提供的Accept-Language来改变返回给浏览器的内容。

我有以下configuration:

 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-LocationVaryTCN响应标题指示MultiViews正确地踢入。

我已经将英文configuration为在浏览器中显示语言的唯一首选语言; 在请求上设置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). 

为什么不正确的踢英语的内容谈判? 有一些configuration我忽略了吗?

在你的configuration中是否存在适当的语言/扩展关系?

 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 

metkat给出的答案暗示了我的麻烦来源。 但是,我决定采用不同的方法。 这是我的configuration有:

 <FilesMatch ".+\.ph(p[345]?|t|tml)$"> SetHandler application/x-httpd-php </FilesMatch> 

这就是我把它改成:

 <FilesMatch ".+\.ph(p[345]?|t|tml)(\.[az]{2}|)$"> SetHandler application/x-httpd-php </FilesMatch> 

我基本上添加到文件名的末尾: (\.[az]{2}|) ,这意味着“…后跟一个点和两个字符的范围az OR( | )没有”。 似乎到目前为止工作顺利:)