4

I’m trying to implement iOS Universal Links, I need to serve an apple-app-association file at the root of my WordPress.

How could I serve my apple-app-association file with Content-type: "application/pkcs7-mime" in WordPress?

I tried to directly upload it, but of course it didn't work because I need to modify the Content-type of the apple-app-association to: Content-type: "application/pkcs7-mime"

DiegoQ
  • 1,114
  • 11
  • 20

5 Answers5

6

Since the apple-app-site-association file is not a WordPress file, you have to configure the content type at the server level. This is different depending on environment (Apache vs. nginx, for example). This can be hard, if your host doesn't allow access to low level configuration.

Apache configuration

Modify the /etc/apache2/sites-available/default-ssl (or equivalent) file to include the snippet:

<Directory /path/to/root/directory/>
...
<Files apple-app-site-association>
Header set Content-type "application/pkcs7-mime"
</Files>
</Directory>

nginx configuration

Modify the /etc/nginx/sites-available/ssl.example.com (or equivalent) file to include the location /apple-app-assocation snippet:

server {
   ...
   location /apple-app-site-association {
      default_type application/pkcs7-mime;
   }
}

Source: https://gist.github.com/anhar/6d50c023f442fb2437e1#modifying-the-content-type

In theory I believe it is possible to do the Apache configuration via a .htaccess file, but I've never tried.

You may prefer to look into a free hosted deep link service like Branch (full disclosure: I'm in the Branch team) or Firebase Dynamic Links to handle all of this for you.

Alex Bauer
  • 13,147
  • 1
  • 27
  • 44
  • Thanks, this worked for me. For Apache on Ubuntu I also needed to have the Headers module installed and enabled `$ a2enmod headers` then restart Apache. – rob5408 Mar 05 '18 at 17:36
  • @rob5408, I'm in a similar situation. No idea how to transfer the file to the website root. Any suggestions? – Shyam Jun 29 '18 at 06:30
  • @Shyam Sorry, no. I was in over my head for even my little problem ;) – rob5408 Jul 02 '18 at 21:46
  • what does `/path/to/root/directory/` represent in this scenario exactly? – ewizard Sep 27 '18 at 01:50
5

In case anyone is in the same situation I was where my website is hosted on Bitnami WordPress (e.g. through AWS), your root directory path is /opt/bitnami/apps/wordpress/htdocs. Once you've copied your association file there, the place to make the configuration change for the content type header described in Alex's answer is /opt/bitnami/apps/wordpress/conf/httpd-app.conf. Finally, you'll need to restart Apache for the configuration change to kick in, using the command sudo apachectl -k graceful. You can verify that your setup is correct using this validator tool.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Harley Z.
  • 51
  • 1
  • 1
  • 1
    Funny that the validator tool you referenced is actually better than Apple’s own validator tool. Apple’s tool will often give a JSON parsing error when there is none. – ScottyB Sep 01 '20 at 14:16
  • On latest versions of bitnami Wordpress, use `vi /opt/bitnami/apps/letsencrypt/conf/httpd-app.conf` – Hans Bondoka Apr 20 '23 at 21:10
4

The easiest way to have the apple-app-site-association file delivered with content type application/json or application/pkcs7-mime in Apache is to add an .htaccess file in the same directory with the following contents:

<Files apple-app-site-association>
ForceType application/json
</Files>

or

<Files apple-app-site-association>
ForceType application/pkcs7-mime
</Files>

Then you don't have to add it to your server configuration.

Credit goes to http://redquark.com/wp/?p=209

ScottyB
  • 2,167
  • 1
  • 30
  • 46
1

For AWS / lightsail

you can simply connect via ssh extension from vscode - (just configure ssh config with pem file - should look like this)

Host bitnami-wordpress
  HostName 111.1.1.1 (your external ec2 ip address)
  User bitnami
  IdentityFile /Users/USERNAME/your-ec2-pem-file.pem

Now just open up the /opt/bitnami/wordpress folder enter image description here

enter image description here

Just drag and drop the apple-app-site-association file into this directory

Open .htaccess / add this at bottom.

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

TROUBLESHOOTING if you get a permissions problem saving file - you can save it as a different name. eg. 2.htaccess
open a new terminal - enter image description here

then remove old file (you'll likely need to change permissions to appropriate user. For lightsail - bitnami it's sudo chown -R bitnami:daemon .htaccess

rm .htaccess
mv 2.htaccess .htaccess
(WARNING ONLY FOR AWS) sudo chown -R bitnami:daemon .htaccess
sudo apachectl -k graceful
johndpope
  • 5,035
  • 2
  • 41
  • 43
0

I was able to upload the file after adding the following to wp-config.php file:

define('ALLOW UNFILTERED UPLOADS', true);

Now when I have the file uploaded I again removed the line for security reasons and I can further update the content of apple-app-site-association file through FTP.

Lukas Smilek
  • 41
  • 1
  • 7