RewriteRule循环,内容协商

时间:2015-10-29 15:37:44

标签: .htaccess mod-rewrite

我已经通过Stack Overflow进行了巡视,我没有找到问题的答案(或者至少我没有认出来),所以我决定打开我自己的问题。

我正在尝试建立一个内容协商系统,默认情况下在请求时提供HTML,并在请求时提供RDF / XML。 URI将是非信息源。用户将转到URI,它会根据请求将用户重定向到相应的文件。例如:

GET: http://example.com/ldExamples/catalog_001
Accept: application/rdf+xml

这会导致重定向到http://example.com/ldExamples/catalog_001.rdf

我尝试了几种方法。以下所有代码均来自W3C's Best Practice Recipes for Publishing RDF Vocabularies。这是第一次尝试:

# Turn off MultiViews
Options -MultiViews

# Directive to ensure *.rdf files served as appropriate content type,
# if not present in main apache config
AddType application/rdf+xml .rdf

# Rewrite engine setup
RewriteEngine On
RewriteBase /ldExamples

# Rewrite rule 2: to serve HTML content from class or prop URIs if     requested
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 ^(.+)/$ $1.html [R=303]

# Rewrite rule 3: to serve RDF content is requested
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^(.+)/$ $1.rdf [R=303]

# Choose the default response
# ---------------------------

# Rewrite rule 4: to serve RDF/XML content by default
#RewriteRule ^(.+)/$ $1.rdf [R=303]

# Rewrite rules to serve HTML content by default
RewriteRule ^(.+)/$ $1.html [R=303]

尝试时:

GET: http://example.com/ldExamples/catalog_001/

我收到http://example.com/ldExamples/catalog_001.html.html

使用:

GET: http://example.com/ldExamples/catalog_001/
Accept: application/rdf+xml

我收到http://example.com/ldExamples/catalog_001.html.rdf.html

我知道这与循环有关。我尝试将L添加到[R=303]。后来我发现由于循环,我收到了500内部错误。

为了进一步调试,我尝试删除循环并直接定位URL:

# Turn off MultiViews
Options -MultiViews

# Directive to ensure *.rdf files served as appropriate content type,
# if not present in main apache config
AddType application/rdf+xml .rdf

# Rewrite engine setup
RewriteEngine On
RewriteBase /ldExamples

# Rewrite rule 2: to serve HTML content from class or prop URIs if  requested
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 ^catalog_001$ catalog_001.html [R=303]

# Rewrite rule 3: to serve RDF content is requested
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^catalog_001$ catalog_001.rdf [R=303]

# Choose the default response
# ---------------------------

# Rewrite rule 4: to serve RDF/XML content by default
# RewriteRule ^catalog_001$ catalog_001.rdf [R=303]

# Rewrite rules to serve HTML content by default
RewriteRule ^catalog_001$ catalog_001.html [R=303]

无论http://examples.com/ldExamples/catalog_001.html请求如何,都会返回Accept。我尝试将L添加到[R=303],但也导致500内部错误。

最终,我想要一些变量设置,因为我想提供多个URI和文件。

0 个答案:

没有答案