注意:我假设 WordPress 安装在根文件夹中。
所以如果你想要http://example.com/apple-app-site-association
提供 JSON 内容,例如this https://pastebin.com/vR8jVQB5并具有内容类型 (Content-Type
标题)设置为application/pkcs7-mime
,以下是您可以选择的一些选项:
动态内容
因为您使用的是 WordPress,所以这对您来说可能是比手动编辑更好的选择.htaccess
file.
您可以使用parse_request
钩;这样,您不需要任何自定义(WordPress)重写规则,不需要自定义页面(帖子类型page
),并且您不需要创建任何 JSON 文件。
并确保没有名为apple-app-site-association
在根文件夹中,或具有 slug 的 WordPress 页面中apple-app-site-association
.
所以这将进入您的主题函数文件(例如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;
}
静态内容
将 JSON 数据放入名为的文件中apple-app-site-association
(即没有扩展名)并将文件保存在根文件夹中,您可以在其中看到wp-config.php
and .htaccess
files.
将其添加到您的.htaccess
file:
<Files apple-app-site-association>
Header set Content-Type application/pkcs7-mime
</Files>
If the <Files>
and/or Header
不/不适合你,那么你可以只使用上面的第一个选项,但使用类似readfile()
读取静态文件。
或者(特别是如果您的站点不支持/不支持 URL 重写),您可以创建一个名为的文件夹apple-app-site-association
在根文件夹中并添加index.php
到该文件夹和该文件中:
<?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;