skip to Main Content

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


  1. Chosen as BEST ANSWER
    AddType application/ld+json .jsonld
    
    RewriteEngine On 
    RewriteCond %{HTTP:Accept} application/ld+json
    RewriteRule ^ jsonld.jsonld
    

  2. There’s no need for an Apache <If> expression or Header directive as you can do this using mod-rewrite only. For example:

    RewriteEngine On
    
    RewriteCond %{HTTP:Accept} application/ld+json
    RewriteRule ^ jsonld [T=application/ld+json,END]
    

    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 and jsonld files) then the slash prefix on the substitution string is intentionally omitted.

    The END flag (Apache 2.4) serves to prevent a rewrite loop.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search