Home
Support Forums

Conditional redirect based on 'Accept' header in HTTP request

Dear,

I am trying to create a conditional redirect based on headers. The header in particular is the ‘Accept’ header. When a normal ‘text/html’ media type is used, it should just show the site (e.g. index.html)
but when another media type such as ‘text/turtle’ is used it should redirect to a specific file.

My use case is the creation of a linked data frontend that serves ‘human readable’ documentation when the URL is accessed by a browser and ‘machine readable’ information when accessed by a machine.

The redirect/rewrites that I am trying to create are based from an Apache htaccess file (generated by a tool):

# Rewrite rule to serve RDF/XML content from the vocabulary URI if requested
RewriteCond %{HTTP_ACCEPT} \*/\* [OR]
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^def$ doc/ontology.rdf [R=303,L]

RewriteCond %{HTTP_ACCEPT} \*/\* [OR]
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^def/(.+)$ doc/ontology.rdf [R=303,NE,L]

# Rewrite rule to serve N-Triples content from the vocabulary URI if requested
RewriteCond %{HTTP_ACCEPT} application/n-triples
RewriteRule ^def$ doc/ontology.nt [R=303,L]

RewriteCond %{HTTP_ACCEPT} application/n-triples
RewriteRule ^def/(.+)$ doc/ontology.nt [R=303,NE,L]

# Rewrite rule to serve TTL content from the vocabulary URI if requested
RewriteCond %{HTTP_ACCEPT} text/turtle [OR]
RewriteCond %{HTTP_ACCEPT} text/\* [OR]
RewriteCond %{HTTP_ACCEPT} \*/turtle 
RewriteRule ^def$ doc/ontology.ttl [R=303,L]

RewriteCond %{HTTP_ACCEPT} text/turtle [OR]
RewriteCond %{HTTP_ACCEPT} text/\* [OR]
RewriteCond %{HTTP_ACCEPT} \*/turtle 
RewriteRule ^def/(.+)$ doc/ontology.ttl [R=303,NE,L]

My netlify.toml that I tried to see if something like this was possible (for https://openhps.org/). But it does not seem to work (redirects are parsed correctly according to the build log)

[[redirects]]
  from = "/owl"
  to = "/owl/v1/ontology.ttl"
  status = 200
  force = true
  conditions = {Accept = "text/turtle"}

[[redirects]]
  from = "/owl/v1"
  to = "/owl/v1/ontology.ttl"
  status = 200
  force = true
  conditions = {Accept = "text/turtle"}

[[redirects]]
  from = "/owl"
  to = "/owl/v1"
  status = 200
  force = true

My fear is that conditional headers in redirects are not possible - but I want to confirm this as the documentation only talks about languages, country, roles and cookies. To be clear, the client initiating the request is a fixed w3 specification, so I can not change it to include a cookie or similar.

Note that the ‘headers’ option is for creating additional response headers - so I do not thing that is the thing to use either.

Best,
Maxim

Within the documentation here:

The only conditions that are shown are Role, Language and Country.

Digging a bit deeper you can see that they are the only conditions specified in the redirect parser:

I’d imagine you could perform this with an Edge Handler / Edge Worker though:

thanks for the pointers. Will sign up for the early access of edge handlers. I will update this post once I confirmed it worked (or if I found another solution for users with similar problems)