0

I am trying to setup rewrite rules for a custom homepage because we are planning on using full-screen slides and using a javascript pushstate to update the url instead of using formal WordPress pages. We decided using GET variables would be easiest in order to allow linking to a certain slide on the homepage, but it's not nice looking so I wanted to use rewrite rules in order to make everything slick and SEO friendly.

Here's a few examples of what we are trying to do ("slide" is not a page/post in WP):

Nice URL: example.com/slide/something/
Ugly URL: example.com/?slide=something

Nice URL: example.com/slide/videos/
Ugly URL: example.com/?slide=videos

Now, we have no problem with the ugly URLs, but my rewrite rule for this just isn't working. I have gone through several questions/answers on this but none of the accepted answers have been working. Here's what I have in functions.php:

add_action('init', 'add_rewrite_rules');
function add_rewrite_rules() {
    flush_rewrite_rules();
    add_rewrite_rule(
        '^slide/([^/]*)/?$',
        'index.php?slide=$matches[1]',
        'top'
    );
}

I'm flushing rewrite rules while I try to get this working; I realize I shouldn't have this in there, being called on every refresh if this was a production site.

If I try to go to example.com/slide/something/ I am redirected to the homepage.

  • why not add to your htaccess? – rudtek Jun 14 '17 at 15:58
  • rules should get flushed after new ones are added, not before. – Milo Jun 14 '17 at 16:17
  • Redirecting to htaccess takes me to a 404 unless I direct it to something other than index.php (I'm assuming since WordPress handles everything with index.php) – DavyH Jun 14 '17 at 17:20
1

Two problems I see- rewrite rules need to set query vars that will result in a successful main query. Setting just a custom var like slide doesn't parse to anything WordPress can load. Additionally, slide needs to be added to the recognized query vars for it to get parsed within a rule.

So, what would a rule look like that would load the front page posts in the main query? That's a good question- the posts page is a special case, the absence of any other query vars. I haven't found a way to do that with a rule, though it may exist.

An easier way to do this is with a rewrite endpoint:

function wpd_slide_endpoint(){
    add_rewrite_endpoint( 'slide', EP_ROOT );
}
add_action( 'init', 'wpd_slide_endpoint' );

Keep in mind that if you have code accessing values via $_GET, this still won't work, because WordPress doesn't put query vars there when rules are parsed. You can change the code to use get_query_var, or just assign it before the code tries to access it:

$_GET['slide'] = get_query_var('slide');
0

@Milo was two minutes faster, therefor I will just add to his answer, that using a CPT for slides might be the simplest way to get pretty urls for the slides, and in addition it will be much easier to SEO the slides.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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