url rewriting - Custom rewrite rule sends 404 header on multisite

I have a custom rewrite rule which rewrites wp-contentthemesmy-thememanifest.json to index.php?manifest=true. This

I have a custom rewrite rule which rewrites /wp-content/themes/my-theme/manifest.json to /index.php?manifest=true. This generates some JSON code for progressive web app functionality, and generally works great.

On a multisite install, however, the manifest loads correctly, but still sends a 404 header. I verified that this specific theme does work fine with a normal WordPress site, but in a multisite install, it always seems to send the 404 header.

Some notes:

  • I've tried using status_header(200), and header("HTTP/1.1 200 OK") manually, but neither of those make a difference
  • I've tried setting $wp->is_404 to false, but that does nothing
  • I've flushed the permalinks, but that does nothing

Rewrite Rule PHP:

// set up rewirte rules for PWA functionality
function fvpd_pwa_rewrite_rules() {
    add_rewrite_endpoint("manifest", EP_NONE);
    add_rewrite_rule(substr(parse_url(get_template_directory_uri(), PHP_URL_PATH), 1) . "/manifest\.json$", "index.php?manifest=true", "top");
}
add_action("init", "fvpd_pwa_rewrite_rules");

manifest.json PHP:

// construct a manifest when the user visits {theme_folder}/manifest.json
function fvpd_construct_manifest() {
    if (get_query_var("manifest")) {
        header("Content-Type: application/json");

        $name             = fvpd_get_field("full_name", "pwa");
        $short_name       = fvpd_get_field("short_name", "pwa");
        $background_color = fvpd_get_field("background_color", "pwa");
        $theme_color      = fvpd_get_field("theme_color", "pwa");

        $manifest = array(
            "start_url"        => "/",
            "display"          => "standalone",
            "name"             => $name ? $name : "Fox Valley Park District - DEV",
            "short_name"       => $short_name ? $short_name : "FVPD",
            "background_color" => $background_color ? $background_color : "#E58F1A",
            "theme_color"      => $theme_color ? $theme_color : "#E58F1A",
            "icons"            => array(
                array(
                    "src"   => get_theme_file_uri("assets/media/android/splash-icon-512x512.png"),
                    "type"  => "image/png",
                    "sizes" => "512x512",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-192x192.png"),
                    "type"  => "image/png",
                    "sizes" => "192x192",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-144x144.png"),
                    "type"  => "image/png",
                    "sizes" => "144x144",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-96x96.png"),
                    "type"  => "image/png",
                    "sizes" => "96x96",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-72x72.png"),
                    "type"  => "image/png",
                    "sizes" => "72x72",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-48x48.png"),
                    "type"  => "image/png",
                    "sizes" => "48x48",
                ),
            ),
        );

        echo json_encode($manifest); exit;
    }
}
add_action("wp", "fvpd_construct_manifest");

URL in questions:

.json

This does not affect my other custom rewrite, which points /offline/ to load a custom template.

/

Why is the 404 header being sent, and how can I fix it?


UPDATE 1:

I tried some of the advice below, but to no avail. I do have some additional information that may help, however. I tested changing the rewrite to /manifest.json instead of /wp-content/themes/my-theme/manifest.json, and that actually worked! This gave me an idea, and so I tried the following:

| URL                        | Status | Folder Exists | WordPress Folder |
|:--------------------------:|:------:|:-------------:|:----------------:|
| /manifest.json             | 200    | N/A           | N/A              |
| /wp-admin/manifest.json    | 404    | true          | true             |
| /wp-content/manifest.json  | 404    | true          | true             |
| /wp-includes/manifest.json | 404    | true          | true             |
| /cgi-bin/manifest.json     | 403    | true          | false            |
| /custom/manifest.json      | 200    | false         | false            |
| /custom/manifest.json      | 200    | true          | false            |

It seems that if the rewrite is returning a 404 header only when set to an existing WordPress folder. My suspicion is that the rewrite engine is partially ignoring rules to /wp-* folders.

I may end up just keeping the rewrite at /manifest.json, but that creatures a problem if in the future we where to set up additional sites like /, although that could probably be fixed by rewriting to the WordPress root instead of the server root.

UPDATE 2: .htaccess contents is visible here:

I have a custom rewrite rule which rewrites /wp-content/themes/my-theme/manifest.json to /index.php?manifest=true. This generates some JSON code for progressive web app functionality, and generally works great.

On a multisite install, however, the manifest loads correctly, but still sends a 404 header. I verified that this specific theme does work fine with a normal WordPress site, but in a multisite install, it always seems to send the 404 header.

Some notes:

  • I've tried using status_header(200), and header("HTTP/1.1 200 OK") manually, but neither of those make a difference
  • I've tried setting $wp->is_404 to false, but that does nothing
  • I've flushed the permalinks, but that does nothing

Rewrite Rule PHP:

// set up rewirte rules for PWA functionality
function fvpd_pwa_rewrite_rules() {
    add_rewrite_endpoint("manifest", EP_NONE);
    add_rewrite_rule(substr(parse_url(get_template_directory_uri(), PHP_URL_PATH), 1) . "/manifest\.json$", "index.php?manifest=true", "top");
}
add_action("init", "fvpd_pwa_rewrite_rules");

manifest.json PHP:

// construct a manifest when the user visits {theme_folder}/manifest.json
function fvpd_construct_manifest() {
    if (get_query_var("manifest")) {
        header("Content-Type: application/json");

        $name             = fvpd_get_field("full_name", "pwa");
        $short_name       = fvpd_get_field("short_name", "pwa");
        $background_color = fvpd_get_field("background_color", "pwa");
        $theme_color      = fvpd_get_field("theme_color", "pwa");

        $manifest = array(
            "start_url"        => "/",
            "display"          => "standalone",
            "name"             => $name ? $name : "Fox Valley Park District - DEV",
            "short_name"       => $short_name ? $short_name : "FVPD",
            "background_color" => $background_color ? $background_color : "#E58F1A",
            "theme_color"      => $theme_color ? $theme_color : "#E58F1A",
            "icons"            => array(
                array(
                    "src"   => get_theme_file_uri("assets/media/android/splash-icon-512x512.png"),
                    "type"  => "image/png",
                    "sizes" => "512x512",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-192x192.png"),
                    "type"  => "image/png",
                    "sizes" => "192x192",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-144x144.png"),
                    "type"  => "image/png",
                    "sizes" => "144x144",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-96x96.png"),
                    "type"  => "image/png",
                    "sizes" => "96x96",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-72x72.png"),
                    "type"  => "image/png",
                    "sizes" => "72x72",
                ),
                array(
                    "src"   => get_theme_file_uri("assets/media/android/launcher-icon-48x48.png"),
                    "type"  => "image/png",
                    "sizes" => "48x48",
                ),
            ),
        );

        echo json_encode($manifest); exit;
    }
}
add_action("wp", "fvpd_construct_manifest");

URL in questions:

https://www.foxvalleyparkdistrict/wp-content/themes/fox-valley-park-district/manifest.json

This does not affect my other custom rewrite, which points /offline/ to load a custom template.

https://www.foxvalleyparkdistrict/offline/

Why is the 404 header being sent, and how can I fix it?


UPDATE 1:

I tried some of the advice below, but to no avail. I do have some additional information that may help, however. I tested changing the rewrite to /manifest.json instead of /wp-content/themes/my-theme/manifest.json, and that actually worked! This gave me an idea, and so I tried the following:

| URL                        | Status | Folder Exists | WordPress Folder |
|:--------------------------:|:------:|:-------------:|:----------------:|
| /manifest.json             | 200    | N/A           | N/A              |
| /wp-admin/manifest.json    | 404    | true          | true             |
| /wp-content/manifest.json  | 404    | true          | true             |
| /wp-includes/manifest.json | 404    | true          | true             |
| /cgi-bin/manifest.json     | 403    | true          | false            |
| /custom/manifest.json      | 200    | false         | false            |
| /custom/manifest.json      | 200    | true          | false            |

It seems that if the rewrite is returning a 404 header only when set to an existing WordPress folder. My suspicion is that the rewrite engine is partially ignoring rules to /wp-* folders.

I may end up just keeping the rewrite at /manifest.json, but that creatures a problem if in the future we where to set up additional sites like https://example/second-site/, although that could probably be fixed by rewriting to the WordPress root instead of the server root.

UPDATE 2: .htaccess contents is visible here: https://gist.github/JacobDB/1531b75c8b8c79117516019225bb7732

Share Improve this question edited Jun 17, 2019 at 18:55 JacobTheDev asked Apr 8, 2019 at 16:08 JacobTheDevJacobTheDev 1,2535 gold badges18 silver badges37 bronze badges 6
  • Would each user get a unique manifest.json file? Or is it the same manifest.json file going out to everyone and if so, how often is that data changing? – ChristopherJones Commented Jun 13, 2019 at 14:58
  • 1 It's one manifest for everyone that visits the site, but it's editable by the admins -- they can change the color or name in the back-end. – JacobTheDev Commented Jun 14, 2019 at 16:25
  • @JacobTheDev are you using nginx or apache? If it's Apache, please paste your .htaccess file here. Also, let me know if you are allowed to ssh into your server. – filipecsweb Commented Jun 17, 2019 at 16:01
  • Using apache, added a link to my .htaccess. I don't have SSH access directly, but I may be able to get it if it's absolutely necessary. – JacobTheDev Commented Jun 17, 2019 at 18:56
  • FYI, the original URL to the manifest.json in you question is working for me. – Nathan Powell Commented Jun 17, 2019 at 19:56
 |  Show 1 more comment

4 Answers 4

Reset to default 1 +50

Basically, you are doing one thing wrong, which is actually having a file named manifest.json inside /wp-content/themes/your-theme.

So, first, delete your file /wp-content/themes/your-theme/manifest.json.

Then, in your functions.php file you can have your rewrite rule as:

function fvpd_pwa_rewrite_rules() {
    add_rewrite_endpoint( "manifest", EP_NONE );
    add_rewrite_rule( substr( parse_url( get_template_directory_uri(), PHP_URL_PATH ), 1 ) . "/manifest.json/?$", "index.php?manifest=true", "top" );
}

add_action( "init", "fvpd_pwa_rewrite_rules" );

And your json content as:

// construct a manifest when the user visits {theme_folder}/manifest.json
function fvpd_construct_manifest() {
    if ( get_query_var( "manifest" ) ) {
        $name             = fvpd_get_field( "full_name", "pwa" );
        $short_name       = fvpd_get_field( "short_name", "pwa" );
        $background_color = fvpd_get_field( "background_color", "pwa" );
        $theme_color      = fvpd_get_field( "theme_color", "pwa" );

        $manifest = array(
            "start_url"        => "/",
            "display"          => "standalone",
            "name"             => $name ? $name : "Fox Valley Park District - DEV",
            "short_name"       => $short_name ? $short_name : "FVPD",
            "background_color" => $background_color ? $background_color : "#E58F1A",
            "theme_color"      => $theme_color ? $theme_color : "#E58F1A",
            "icons"            => array(
                array(
                    "src"   => get_theme_file_uri( "assets/media/android/splash-icon-512x512.png" ),
                    "type"  => "image/png",
                    "sizes" => "512x512",
                ),
                array(
                    "src"   => get_theme_file_uri( "assets/media/android/launcher-icon-192x192.png" ),
                    "type"  => "image/png",
                    "sizes" => "192x192",
                ),
                array(
                    "src"   => get_theme_file_uri( "assets/media/android/launcher-icon-144x144.png" ),
                    "type"  => "image/png",
                    "sizes" => "144x144",
                ),
                array(
                    "src"   => get_theme_file_uri( "assets/media/android/launcher-icon-96x96.png" ),
                    "type"  => "image/png",
                    "sizes" => "96x96",
                ),
                array(
                    "src"   => get_theme_file_uri( "assets/media/android/launcher-icon-72x72.png" ),
                    "type"  => "image/png",
                    "sizes" => "72x72",
                ),
                array(
                    "src"   => get_theme_file_uri( "assets/media/android/launcher-icon-48x48.png" ),
                    "type"  => "image/png",
                    "sizes" => "48x48",
                ),
            ),
        );

        wp_send_json( $manifest );
    }
}

add_action( "wp", "fvpd_construct_manifest" );

Notice the usage of the WordPress function wp_send_json() which handles for you necessary headers, json converting, etc.

Make sure you flush your permalinks and test the URL which will be something like http://localhost/wp-content/themes/your-theme/manifest.json.

If the above still doesn't solve your issue it means you are also having trouble setting your web server correctly, NGINX or APACHE, following WordPress standards.

I ran into a similar issue some time back. I came up with an answer that does not fix the 404 issue, but takes a different approach in solving the issue, assuming your content of this json file is not set per user.

Rather than having PHP run inside of that json file and changing the header of the file to reflect as much, why not have a manifest.php file sit next and write the manifest.json file for you:

themes/fox-valley-park-district/
  \__ manifest.php
  \__ manifest.json

Do all the work laid out above in your php file and remove the add_action hook call in there. Maybe your php file looks like like:

<?php
  function fvpd_construct_manifest() {

    // Compile what you need

    return $manifest_output;
  }

  $manifest_json_file = 'manifest.json';
  $json_data = fvpd_construct_manifest();
  $json_data_encoded = json_encode($json_data);

  // You may need to deal with permission issues on that .json file
  file_put_contents(__DIR__ .'/'.$manifest_json_file, $json_data_encoded);

And then run a cron to hit that manifest.php. Again, it's a different approach and answer, but does not answer the specific 404 question. If I am way off here....I'd be happy to remove the answer.

Hope it helps, Good luck!!

I think you just have a typo. Changing the following made this work for me on multisite.

Try changing this:

    add_rewrite_rule(substr(parse_url(get_template_directory_uri(), PHP_URL_PATH), 1) . "/manifest\.json$", "index.php?manifest=true", "top");

to this:

    add_rewrite_rule(substr(parse_url(get_template_directory_uri(), PHP_URL_PATH), 1) . "/manifest.json", "index.php?manifest=true", "top");

I think that's because .json is considered to be a "static file". Which means PHP isn't triggered with the request. This kind of routing likely needs to occur at a server level.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745392823a4625741.html

相关推荐

  • url rewriting - Custom rewrite rule sends 404 header on multisite

    I have a custom rewrite rule which rewrites wp-contentthemesmy-thememanifest.json to index.php?manifest=true. This

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信