How does one make a URL return dynamic JSON with custom Content Type?

I asked this on stack overflow as well. You can find that answer here:

https://stackoverflow.com/a/56801044/343292

I’ll copy and paste the answer below incase the link ever breaks. It worked for me. I used the dynamic way.

ANSWER FROM @Sally CJ


Note: I’m assuming WordPress is installed in the root folder.

So if you want http://example.com/apple-app-site-association to serve a JSON content like this and have the content type (Content-Type header) set to application/pkcs7-mime, here are some options you can choose from:

Dynamic Content

Because you’re using WordPress, this might be a better option for you than manually editing the .htaccess file.

You can use the parse_request hook; this way, you don’t need any custom (WordPress) rewrite rules, no need for a custom Page (post type of page), and you don’t need to create any JSON file.

And make sure there’s no file named apple-app-site-association in the root folder, or a WordPress Page having the slug apple-app-site-association.

So this would go in your theme functions file (e.g. wp-content/themes/your-theme/functions.php):

<?php
add_action( 'parse_request', 'serve_apple_app_site_association', 0 );
function serve_apple_app_site_association( $wp ) {
    // Check if the request is /apple-app-site-association
    if ( 'apple-app-site-association' !== $wp->request ) {
        return;
    }

    // Array version of the JSON data.
    $data = array(
        'applinks'    => array(
            'apps'    => array(),
            'details' => array(
                array(
                    'appID' => '9JA89QQLNQ.com.apple.wwdc',
                    'paths' => array(
                        '/wwdc/news/',
                        '/videos/wwdc/2015/*',
                    ),
                ),
                array(
                    'appID' => 'ABCD1234.com.apple.wwdc',
                    'paths' => array(
                        '*',
                    ),
                ),
            ),
        ),
    );

    // Send headers.
    status_header( 200 );
    nocache_headers();
    header( 'Content-Type: application/pkcs7-mime' );

    // And serve the JSON data.
    echo wp_json_encode( $data );
    exit;
}

Static Content

  1. Place the JSON data in a file named apple-app-site-association (i.e. no extension) and save the file in the root folder where you could see the wp-config.php and .htaccess files.

  2. Add this to your .htaccess file:

<Files apple-app-site-association>
        Header set Content-Type application/pkcs7-mime
</Files>

If the <Files> and/or Header don’t/doesn’t work for you, then you could just use the first option above, but use something like readfile() to read the static file.

Alternatively (and specifically if URL rewriting is not available/supported on your site), you could create a folder named apple-app-site-association in the root folder and add index.php to that folder and in that file:

<?php
header( 'Content-Type: application/pkcs7-mime' );
// Read the static file or just generate a dynamic content.
@readfile( __DIR__ . '/apple-app-site-association.json' );
exit;