Home > Mobile >  When my server receives a request with the header "Accept: application/ld json", return a
When my server receives a request with the header "Accept: application/ld json", return a

Time:10-11

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>

CodePudding user response:

Try this:

<If "%{HTTP:Accept} =~ /application\/ld\ json/">
      RewriteEngine On
      RewriteCond %{REQUEST_URI} !^/jsonld$
      RewriteRule ^ /jsonld [L]
</If>
Header always set Content-Type "application/ld json" "expr=%{REQUEST_URI} =~ /^\/jsonld$/"

Please read this for more info about If statements in Apache.

CodePudding user response:

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.

  • Related