0

I have a page /video which has a javascript app running on it. The javascript reads the url fragments and loads a video. ie /video/my-first will load the video titled My First.

My issue here is with the permalinks. WordPress identifies the multi fragment url and tries to find a match. I have configured an add_rewrite_rule to skip anything with ^video which works fine for the site.

I effectively need any url with /video to route to the same location whether it is /video, /video/one, /video/35 or /video/really-really-long-slug-with-trailing-slash/. The url must be preserved, as my javascript needs to know the slug to function correctly.

The problem with my rewrite is Google appears to pick up on the redirect here and will not fetch the rendered page. It lists it as a 301, permanently redirected.

Googlebot type: Desktop (render requested) Redirected on Tuesday, April 10, 2018 at 4:01:40 AM PDT

I am not a WordPress developer (I wrote the javascript app), so I was hoping there would be a way to exclude the Video page from the permalinks. Currently it is a WordPress page entity, but I have no issue using a single php page in the theme and linking directly to it via a menu.

2 Answers 2

1

It's a complex question. First of all - its works "OK", from the point of user with no javascript and google-bot. I would suggest fixing this situation in a different way, rather then using page and subpages:

  1. Create, not a page but post type video with characteristics:

    1. Non-Public
    2. Publicly queryable
    3. No Rewrites
  2. Add Query Rule

add_action( 'init', function() {
    add_rewrite_rule( '^videos/?', 'index.php?post_type=video', 'top' );
});
  1. Add Custom Parse Query filter.
add_action( 'init', function(){
    if ( is_admin() )
        return;
    add_filter( 'parse_query', function( WP_Query $wp_query) : WP_Query {
        if ( 'video' === $wp_query->get('post_type')) {
            $wp_query->set('nopaging', true);
        }
        return $wp_query;
    });
});

And its done. Well, not really done. You will need to generate some data attributes for videos on your page-app.

1
  • Unfortunately I am trying to avoid writing a redirect at all costs. Thank you for your response though.
    – hsimah
    Apr 15, 2018 at 5:02
0

So, not being a WordPress developer seems to have been my issue here. I did not know that you could create a single-{post type}.php template and WordPress would render that post type using that page. It's so simple and yet I over-complicated the whole thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.