I want to be able to do the following:
When my Apache server receives a request with the header:
Accept: application/ld+json
I want to return the file "./jsonld", with the header
ContentType: application/ld+json
This is what I have so far :
<If "%{HTTP:Accept} =~ m#application/ld+json#">
RewriteEngine On
RewriteRule ^ /jsonld [L]
Header always set Content-Type "application/ld+json"
</If>
2
Answers
There’s no need for an Apache
<If>
expression orHeader
directive as you can do this using mod-rewrite only. For example:Every request that sends the appropriate
Accept
header is rewritten to/jsonld
. This includes requests for/jsonld
itself (to ensure the correct mime-type is sent back in the response).The
T
flag forces the mime-type (Content-Type
header) that will be sent with the response.Assuming this is all located in the document root (
.htaccess
andjsonld
files) then the slash prefix on the substitution string is intentionally omitted.The
END
flag (Apache 2.4) serves to prevent a rewrite loop.