Apache httpd:将全局重定向的内容类型更改为 application/json

Apache httpd: Change Content-Type of global redirect to application/json

我的机器上有一个 Apache 2.2 http 服务器 运行 用于测试目的。它在特定端口上所做的只是重定向到外部站点。

我的 httpd.conf 文件中的 VirtualHost 部分如下所示:

<VirtualHost *:8001>
    ServerName localhost
    Redirect / http://www.google.com
    LogLevel debug
</VirtualHost>

现在当我这样做时,生成的 HTTP 响应有

Content-Type: text/html; charset=iso-8859-1

但我希望内容类型为 application/json。有什么办法可以实现吗?

我发现 this 个问题的答案是使用 Header set Content-type ...,但这对我来说没有任何改变。

您必须使用 "always" 修饰符以确保它在所有情况下都会发生,在这种情况下您需要它。

<VirtualHost *:8001>
    ServerName localhost
    Redirect / http://www.google.com/
    Header always set Content-Type "application/json"
</VirtualHost>

你会得到类似的东西:

HTTP/1.1 302 Found
Date: Wed, 07 Feb 2018 18:30:42 GMT
Server: Apache
Content-type: application/json
Location: http://www.google.com/