使用 mod_rewrite / RewriteCond 规则进行内容协商

Nan*_*ana 5 apache mod-rewrite content-negotiation github-pages linked-data

我有一个用例是在 Github 页面中托管一组文件(具有不同序列化格式的相同 RDF 内容,例如 RDF/XML、Turtle 和 JSON-LD),并使用w3id URL作为永久标识符。

此外,我想在该永久 URL 上进行内容协商。如果我将文件托管在 Apache 服务器中,这将是微不足道的,但不幸的是 Github 页面不支持内容协商。所以我想看看我可以在多大程度上使用 URL 重写规则来做到这一点。

所以这个想法类似于以下内容。

GET http://w3id.org/foo  -- redirect to --> http://foo.git.io/content.ttl
Accept: text/turtle

GET http://w3id.org/foo  -- redirect to --> http://foo.git.io/content.jsonld
Accept: application/ld+json
Run Code Online (Sandbox Code Playgroud)

目前我的规则如下所示。

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_ACCEPT} ^.*application/rdf\+xml.* 
RewriteRule ^foaf$ http://nandana.github.io/foaf/card.rdf [R=303,L]
RewriteCond %{HTTP_ACCEPT} ^.*text/turtle.* 
RewriteRule ^foaf$ http://nandana.github.io/foaf/card.ttl [R=303,L]
RewriteCond %{HTTP_ACCEPT} ^.*application/ld\+json.* 
RewriteRule ^foaf$ http://nandana.github.io/foaf/card.jsonld [R=303,L]
RewriteRule ^foaf$ http://nandana.github.io/foaf/card.html [R=303,L]
Run Code Online (Sandbox Code Playgroud)

尽管这适用于大多数情况,但在某些极端情况下会中断。例如,如果有一个像下面这样的接受头

Accept: application/rdf+xml;q=0.9,text/turtle
Run Code Online (Sandbox Code Playgroud)

这将返回 application/rdf+xml (因为第一个规则匹配),尽管根据内容协商它应该返回乌龟。有谁知道改进规则来处理这种极端情况的方法?

Sti*_*yes 0

我认为您必须为实际文件制作 0 字节虚拟文件,例如等foo.ttlfoo.jsonld并确保它们的类型是用 声明的AddType,然后常规内容协商可以适用于http://example.com/foo

但不是提供 0 字节文件,而是RewriteRule foo.ttl为每个文件提供 etc,以便将它们重定向到“真实”位置。是的,相当冗长,但即使对于Accept具有多种类型的复杂标头,也应该可以正常工作q=