使用 MultiViews 配置 Apache 语言内容协商

使用 MultiViews 配置 Apache 语言内容协商

我正在尝试获取MultiViewsApache 中的选项,以根据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已正确启动。TCNMultiViews

我已将英语配置为浏览器中显示语言的唯一首选语言;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).

为什么语言内容协商无法正确启动?我是否忽略了某些配置?

答案1

您的配置中是否存在适当的语言/扩展关系?

AddLanguage en .en
LanguagePriority en fr de
ForceLanguagePriority Fallback

答案2

顺便说一句,我遇到了类似的问题,使用了正确的 AddLanguage 等指令。最后我意识到这个问题是 PHP 特有的。

由于某种原因,我在 FilesMatch 指令中使用了 SetHandler:

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

切换到简单的 AddType 解决了该问题:

AddType application/x-httpd-php .php

答案3

metkat 给出的答案也暗示了我的麻烦的根源。然而,我决定采用不同的方法。这是我的配置:

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

我把它改成了:

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

我基本上在文件名末尾添加了:(\.[a-z]{2}|),这意味着“...后跟一个点和两个 az 范围内的字符或(|)无”。到目前为止似乎运行顺利 :)

相关内容