指定并测试htaccess HTTP_ACCEPT

时间:2018-08-22 03:36:47

标签: .htaccess http-redirect http-accept-header

当我用{p>

.htaccess

这有效

curl -H "Accept: application/rdf+xml" -L http://localhost/v0.1
curl -H "Accept: application/rdf+xml" -L http://localhost/v1.0

但这不是

RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/ontology/cocoon.rdf [R=308,L]

没有以下重定向,它将返回:

RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.xml [R=308,L]

完整文档为:

$ curl -H "Accept: application/rdf+xml" http://localhost/v1.0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   358  100   358    0     0  23866      0 --:--:-- --:--:-- --:--:-- 23866<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>308 Permanent Redirect</title>
</head><body>
<h1>Permanent Redirect</h1>
<p>The document has moved <a href="https://miranda-zhang.github.io/cloud-computing-schema/v1.0/index-en.html">here</a>.</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at localhost Port 80</address>
</body></html>

1 个答案:

答案 0 :(得分:0)

我想我已经发现您的文件出了什么问题。您假设为第一个“ RewriteRule”编写的“ RewriteCond”也适用于第二个。

但是,情况并非如此:您应该为第二条规则重复条件。例如,您有

RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^v0\.1/?$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/index.htm [R=308,NE,L]
RewriteRule ^v1\.0/?$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/index-en.html [R=308,NE,L]

但是为了正常工作,您应该拥有

RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/index.htm [R=308,NE,L]

RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/index-en.html [R=308,NE,L]

如果不这样做,那么它将采用无条件的规则,并且每次在您请求“ v1.0”时都会提供HTML,就像在curl命令中一样。

我已经在本地测试了以下文件,并成功运行:

Options +FollowSymLinks -MultiViews

AddType application/rdf+xml .rdf
AddType application/rdf+xml .owl
AddType text/turtle .ttl
AddType application/n-triples .n3
AddType application/ld+json .json

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]

RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/index.htm [R=308,NE,L]

RewriteCond %{HTTP_ACCEPT} !application/rdf\+xml.*(text/html|application/xhtml\+xml)
RewriteCond %{HTTP_ACCEPT} text/html [OR]
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.*
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/index-en.html [R=308,NE,L]

RewriteCond %{HTTP_ACCEPT} application/ld\+json
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.json [R=308,L]

RewriteCond %{HTTP_ACCEPT} \*/\* [OR]
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/ontology/cocoon.rdf [R=308,L]

RewriteCond %{HTTP_ACCEPT} \*/\* [OR]
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.xml [R=308,L]

RewriteCond %{HTTP_ACCEPT} application/n-triples
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.nt [R=308,L]

RewriteCond %{HTTP_ACCEPT} text/turtle [OR]
RewriteCond %{HTTP_ACCEPT} text/\* [OR]
RewriteCond %{HTTP_ACCEPT} \*/turtle
RewriteRule ^v0\.1$ https://miranda-zhang.github.io/cloud-computing-schema/v0.1/ontology/cocoon.ttl [R=308,L]

RewriteCond %{HTTP_ACCEPT} text/turtle [OR]
RewriteCond %{HTTP_ACCEPT} text/\* [OR]
RewriteCond %{HTTP_ACCEPT} \*/turtle
RewriteRule ^v1\.0$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.ttl [R=308,L]

RewriteCond %{HTTP_ACCEPT} .+
RewriteRule ^$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/406.html [R=406,L]

# Default response
RewriteRule ^$ https://miranda-zhang.github.io/clo

这样做时:

curl -sH "Accept:text/turtle" http://localhost/redirectTest2/v1.0

我得到:

<html><head>
<title>308 Permanent Redirect</title>
</head><body>
<h1>Permanent Redirect</h1>
<p>The document has moved <a href="https://miranda-zhang.github.io/cloud-computing-schema/v1.0/ontology.ttl">here</a>.</p>
<hr>
<address>Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.24 Server at localhost Port 80</address>
</body></html>